- in
Javascript
To obtain an object attribute or method, use the dot or parenthesis method, for example:
const obj = {
a: 'a'
}
obj.a // 'a'
obj.b // undefined
Copy the code
- But in the
Typescript
In, sometimes prompt likeProperty 'value' does not exist on type 'Object'
This is becauseTypescript
No properties are defined in this object when code checks are performed - Solution:
- Sets the object type to
any
, such as:
const obj:any = { a: 'a' } obj.b = 'b' Copy the code
- Define attributes that an object has through an interface, for example:
const obj:ValueObject = { a: 'a'} interface ValueObject { value? : string } obj.b ='b' Copy the code
- Enforce using assertions
const obj = { a: 'a' } (obj as any).b = 'b' Copy the code
- Sets the object type to