Difference between const and readonly in typescript

后端 未结 5 886
醉酒成梦
醉酒成梦 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条回答
  •  醉酒成梦
    2021-02-01 12:27

    both:

    • can be changed (for example by .push() if array)

    const:

    • can't be reassigned
    • used for variables

    readonly:

    • can be reassigned but only inside constructor
    • used for properties (class member)

提交回复
热议问题