So, I’m trying to index an object with a string. It’s not going very well. At first when I tried to do this, I got an error that stated that the object’s class didn’t have an index signature, so I added one. This worked just fine, until it just stopped working for no apparent reason. After a while, the index signature that I added gave the error, “IndexSignature not supported.” This didn’t happen before, so I have no clue why it’s happening. The weird thing is that I can use strings to index the object, meaning it works just fine, but when I do I get an error that says, “Dereferencing null/undefined value.” Also, for some reason when I try to print or use the keys of an object that was a child of a class by using the Object.keys()
function, it doesn’t work, but when I try with a normal object, it works just fine. The weird thing is that then I print the type of the array, it returns what you would expect (it returns "object"
).
Here are some examples of what I am talking about:
“IndexSignature not supported” Error:
interface ExampleInterface {
[key: string]: any
}
//% blockNamespace=tweenExtension
class ExampleClass implements ExampleInterface {
[key: string]: any // Error: "IndexSignature not supported"
exampleProperty1?: Vector2
exampleProperty2?: Vector2
exampleProperty3?: Vector2
exampleProperty4?: Vector2
exampleProperty5?: Vector2
arrayOfExampleProperties: { examplePropertyIndex: number, value: Vector2 }[] = []
}
Object.keys() function issue:
class ExampleClass {
x: number
y: number
constructor(_x: number, _y: number) {
this.x = _x
this.y = _y
}
}
console.log(Object.keys(new ExampleClass(5,5))) // Expectation: Returns "[x,y]", Reality: Returns nothing. Absolutely nothing.
let example = new ExampleClass(3,4)
console.log(Object.keys(example)) // Expectation: Returns "[x,y]", Reality: Returns nothing.
let exampleArray = Object.keys(example)
console.log(exampleArray[0]) // Expectation: Returns "x", Reality: Returns nothing.
console.log(exampleArray[0] + "tale") // Expectation: Returns "xtale", Reality: Returns "undefinedtale"
let exampleObject = {x: 5, y: 5}
console.log(Object.keys(exampleObject)) // Expectation: Returns "[x,y]", Reality: Returns "[x,y]"
let exampleArray2 = Object.keys(exampleObject)
console.log(exampleArray2[0]) // Expectation: Returns "x", Reality: Returns "x"
console.log(exampleArray2[0] + "tale") // Expectation: Returns "xtale", Reality: Returns "xtale"
Any and all help is greatly appreciated. Thanks in advance.