Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any name for index type signatures like TS (not just "key"). #1972

Open
trusktr opened this issue Jul 12, 2021 · 5 comments
Open

Any name for index type signatures like TS (not just "key"). #1972

trusktr opened this issue Jul 12, 2021 · 5 comments

Comments

@trusktr
Copy link
Member

@trusktr trusktr commented Jul 12, 2021

Input:

class MyClass {
    [coolKeys: number]: boolean
}

const o = new MyClass 

o[1] = true

TypeScript playground (works)

AssemblyScript playground (error)

The error had me thinking that it was an unsupported feature:

;; ERROR TS1005: 'key' expected.
;; 
;;      [coolKeys: number]: boolean

I thought "a key is required here? I put a key there, but it must not support it".

I didn't realize that it is saying I need to actually name my key "key". Changing it to "key" gets past that error, although in the following example it has some other error I don't understand:

AssemblyScript playground (no more "'key' expected" error)

In asdom I did not have the new error that the previous example has, and it all is working well (NodeList bindings with [] indexed access).

@willemneal
Copy link
Member

@willemneal willemneal commented Jul 12, 2021

As far as I'm aware, the error is because the parser expects key, but the compiler ignores it since it will use the operator overload method to check the type of the key.

@trusktr
Copy link
Member Author

@trusktr trusktr commented Jul 12, 2021

@willemneal In asdom I am not having that error, and I am using both, which is working well. Without the index signature, TypeScript will complain and VS Code will show red squigglies.

Here is the implementation (see that it has both):

https://github.com/lume/asdom/blob/01a9dc3c6259a9418a50f53766b68c54bb47f5c1/assembly/NodeList.ts

And here is the usage (no errors in VSCode or AS):

https://github.com/lume/asdom/blob/01a9dc3c6259a9418a50f53766b68c54bb47f5c1/example/assembly/index.ts#L89-L91

@trusktr
Copy link
Member Author

@trusktr trusktr commented Jul 12, 2021

Oooh, the second error is ERROR TS2542: Index signature in type 'module/MyClass' only permits reading.. In the asdom example I'm only reading (and it is meant to be read-only in that case too), so no wonder I didn't see that error there.

Here is the corrected example with no errors: playground.

@willemneal
Copy link
Member

@willemneal willemneal commented Jul 12, 2021

Looking at your code, just wanted to mention that currently ! results in a runtime check. So another good feature request is to add a compile flag to just treat it like TS does.

@trusktr
Copy link
Member Author

@trusktr trusktr commented Jul 12, 2021

Plus in TypeScript this is how one actually makes the index type read-only: readonly [key: number]: Node | null. It needs some alignment TLC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants