In a TypeScript file, I have defined a 3D array:
var myArr = ['one', [[19, 1], [13, 1], [86, 1], [12, 2]], 'two', [[83, 1], [72, 1], [16, 2]], 'three', [[4, 1]]]; function testArray(){ console.log(myArr[1].length); }
I get a warning under the length property:
Property 'length' does not exist on type '{}'
Is there something I can do to remove this warning?
The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.
The error "Property does not exist on type 'never'" occurs when we forget to type a state array or don't type the return value of the useRef hook. To solve the error, use a generic to explicitly type the state array or the ref value in your React application.
I read a similar post here: How can I stop "property does not exist on type JQuery" syntax errors when using Typescript?
Which explains that I can cast to <any>.
This worked for me:
function testArray(){ console.log((<any>myArr[1]).length); }
Option 1: upgrade to bleeding-edge compiler and get union types
Option 2: Add a type annotation to the variable declaration:
var myArr: any[] = ['one', [[19, 1], [13, 1], [86, 1], [12, 2]], 'two', [[83, 1], [72, 1], [16, 2]], 'three', [[4, 1]]]; function testArray(){ console.log(myArr[1].length); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With