admin管理员组文章数量:1418103
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
本文标签: Typescript ReadonlyltTgt not working on primitivesStack Overflow
版权声明:本文标题:Typescript Readonly<T> not working on primitives - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745269340a2650793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论