I'm practicing TS and I want to pass an array of strings (each string has the property name pair in the form "name=property") to an object, and it works but it shows me an error if I want to print a single property:
var array = ["nombre=jorge","apellido=balsamo","dni=45869202"]
var obj = new Object();
array.forEach(index => {
Object.defineProperty(obj,index.split("=")[0],{ value: `${index.split("=")[1]}`, writable: true})
})
console.log(obj.nombre)
"Property 'name' does not exist on type 'Object'." that error throws me, but in JS this works fine, I guess it's because of the strong typing but I don't know how to solve this, I could add something like an interface so that it recognizes that the object contains these properties but if the array were bigger and had More properties than just 3 I don't know how to implement it.
obj
You did not explicitly define the name property for the object so that intellisense does not know that itobj
has this property, which is why it throws an error. To "skip" this case, you can always access the properties of the objects in the following way: