In TypeScript, Property ‘value’ does not exist on type ‘Object’ when an Object attribute is retrieved as JS. The specific code is as follows:

var obj: Object = Object.create(null); obj.value = "value"; //[ts] Property 'value' does not exist on type'Object'.Copy the code

This is because Typescript does not define properties on the object when performing code checks. There are several ways to resolve this error:

1. Set the object type to any

This is a very efficient way to access and modify any property without compiling errors. The specific code is as follows:

var obj: any = Object.create(null);
obj.value = "value";
Copy the code

2. Obtain object attributes using characters

This is a bit of a hack, but it still solves compilation errors. The specific code is as follows:

var obj: Object = Object.create(null);
obj["value"] = "value";
Copy the code

3. Define attributes of objects through interfaces

Although more complicated, but is the most advocated solution. After an object is declared through an interface, the property values it has are obvious. The specific code is as follows:

var obj: ValueObject = Object.create(null); obj.value = "value"; interface ValueObject { value? : string }Copy the code

4. Enforce using assertions

After an assertion is declared, the compiler executes the assertion type. The specific code is as follows:

var obj: Object = Object.create(null);
(obj as any).value = "value";
Copy the code