[TypeScript] as const, force immutability for Object type

Posted answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[TypeScript] as const, force immutability for Object type相关的知识,希望对你有一定的参考价值。

Unlike javascript‘s const variable declarations, TypeScript allows you to create fully immutable types. In this lesson, we learn how to create immutable types in TypeScript with the help of as const

 

const user = {
    name: ‘xxx‘,
    education: {
        degree: ‘MSc‘
    }
} as const
const users = [
    ‘a‘,
    ‘b‘
] as const
user.education.degree = "BSc"
users.push(‘c‘)

 

技术图片

以上是关于[TypeScript] as const, force immutability for Object type的主要内容,如果未能解决你的问题,请参考以下文章

[TypeScript] as const, force immutability for Object type

TypeScript 中的“as const”是啥意思,它的用例是啥?

如何在 TypeScript 中打破 ForEach 循环

TypeScript 断言使用

Angular 2 中的 const 和 Typescript

Typescript const枚举问题