• inJavascriptTo 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 theTypescriptIn, sometimes prompt likeProperty 'value' does not exist on type 'Object' This is becauseTypescriptNo properties are defined in this object when code checks are performed
  • Solution:
    • Sets the object type toany, 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