"IndexSignature not supported" error and Object.keys() not working

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.

Unfortunately, makecode uses static typescript, which does not support the behaviour you’re trying to accomplish.

Object.keys(x) is not yet supported when x is dynamically a class type. It is supported when x was created with an object literal (eg., {} or { a: 1, b: "foo" }). The order in which properties are returned is order of insertion with no special regard for keys that looks like integer (JavaScript has really counter-intuitive behavior here). When we support Object.keys() on class types, the order will be the static order of field definition.

I asked something like this a while back, same outcome

2 Likes

Yeah, I figured that was the case. Thanks for your help! :slight_smile: :+1:

1 Like