Difference between const and readonly in typescript

后端 未结 5 889
醉酒成梦
醉酒成梦 2021-02-01 11:47

Constant vs readonly in typescript

Declaring a variable as readonly will not allow us to override even if they are public properties.

How const beha

5条回答
  •  猫巷女王i
    2021-02-01 12:22

    as said in https://www.typescriptlang.org/docs/handbook/interfaces.html#readonly-vs-const

    The easiest way to remember whether to use readonly or const is to ask whether you’re using it on a variable or a property. Variables use const whereas properties use readonly.

    as it displayed in below image if you declare const in defining a property you will get an error https://i.imgur.com/cJfDqh9.png

提交回复
热议问题