Why does the Readonly<T>
utility type only function on non-primitive types? And is there a way for me to have it work also on primitive types?
function inspect<T>(inspector: (val: Readonly<T>) => void): void {
// Do something...
}
function run() {
inspect<string>((str: string) => {
str = `New string` // Can change
})
inspect<number>((num: number) => {
num = 12 // Can change
})
inspect<boolean>((bool: boolean) => {
bool = true // Can change
})
inspect<{str: string, num: number, bool: boolean}>((obj) => {
obj.str = `New string` // Can't change; yields compile time error
obj.num = 12 // Can't change; yields compile time error
obj.bool = true // Can't change; yields compile time error
})
}
Playground
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745269341a4619648.html
评论列表(0条)