#How to make a parameter of type string readonly ?

7 messages · Page 1 of 1 (latest)

dapper sentinel
#

Hey guys, this might seem like a stupid question but how are you meant to make a string readonly in ts ?

I have tried to use Readonly<T> and readonly but that only work for object. What about primitive data type like string and boolean etc... ?

const hello = (name: string)=>{
    name = "CHANGED"; // this line should error out.

    console.log(name); // will log "CHANGED"
}

hello("hello")
#

here is a ts playground if that helps:

here

round sirenBOT
#

@dapper sentinel Here's a shortened URL of your playground link! You can remove the full link from your message.

thibault_06826#0

Preview:```ts
const hello = (name: string) => {
name = "CHANGED"

console.log(name)
}

hello("hello")```

finite sleet
#

It has nothing todo with the string. All function parameters are inherently let instead of const and I don't think there is any way to change this behavior

dapper sentinel
#

ah ok that's a shame. Thanks for the help 🙌

silver zinc
dapper sentinel
#

nice thanks !