6. Use typeof annotations, read-only modifiers

Table of contents

1 Use typeof annotations

2 read-only modifier readonly

2.1 Use in classes

2.2 Use in the interface

2.3 Can be used in many places


1 Use typeof annotations

For example, I want to pass in parameters like the variable p

If you give something other than this, an error will be reported.

can also be used like this

Typeof in ts cannot be followed by a function with an undefined return value type

2 read-only modifier readonly

2.1 Use in classes

A property with a read-only modifier can only be modified in the constructor (or a default value can be set), if you modify it elsewhere, an error will be reported

eadonly can only modify properties but not methods

If you write the default value directly without writing the type, then you can’t modify this value in the constructor, because the type of age at this time is the literal value 18, not number anymore

2.2 Use in the interface

The readonly value can only be given when instantiated, and cannot be changed in other places

2.3 Can be used in many places

such as obj

Guess you like

Origin blog.csdn.net/potato123232/article/details/132044441