-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type checking Javascript object literals is not open-ended in Maps #15618
Comments
This is not related to the objects being open ended. the problem here is in a .ts file, you would have specified that as an explicit type parameter as For the second error, you can work around that by giving /** @type {Array<any>} */
const arr = [...]; |
That workaround would relax the type checking pretty far, since what I am aining for is
The best currently working solution, in terms of readability, seems to be /** @type {Map.<string, {[x:string]: number}>} */
const map = new Map()
.set('a', {x: 1})
.set('b', {y: 2}); |
Based on discussion in #15747, We would like to:
// @ts-check
const map = new Map(/* @type {Array<[string, {x?:number, y?:number}]>} */[
['a', {x: 1}],
['b', {y: 2}]
]);
// @ts-check
/** @type {Array<[string, {x?:number, y?:number}]>} */
const arr = [
['a', {x: 1}],
['b', {y: 2}]
]; |
I've split the suggestion for a cast syntax above into #16345. |
Seen in VS Code 1.12.1 == TypeScript 2.3
In a Javascript context, type-checking with
@ts-check
should treat objects as open maps, according to the wiki page. This does not work if a Map is declared with member objects that have varying properties:The
y: 2
in line 5 raises this error message:The
arr
in line 12 raises a slightly different message:It might be arguable whether this is a type checking bug, or whether the wiki documentation is misleading, as the JS relaxation of checking behaviour should only provide for late-adding members.
The text was updated successfully, but these errors were encountered: