-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Support exact matching array types from JSON files #49194
Comments
Duplicate of #32063. |
This is similar but not the exact same thing. The linked issue is talking about taking an array and getting a single compound literal type from the values (like |
The type can't be |
the solution is simple we got casting to const since typescript 4.5 or 4.7 not sure const literalArray = [1,2] as const // declare const literalArray: readonly [1, 2]; const literalArray = /** @type {const} */ ([1,2]) // declare const literalArray: readonly [1, 2]; js that does not work but should work maybe some one should write a proposal: https://www.typescriptlang.org/play?target=99&jsx=0&ts=4.7.0-beta&filetype=js#code/MYewdgzgLgBGCuBbARgUwE4EF3oIYE8YBeGAbQEYAaAJgF0BuAKFElgBsBLKDXN7PQiQD0AKhEwAAlHwAHVDADeLaAF8YIoTAAUFGrQCUQA const numberArray = [1,2];
const literalArray = /** @type {const} */ (numberArray) // declare const literalArray: number[]; should be readonly [1, 2]; when this would be supported. it got implemented in a way so that you need to indicate that on assignment hope that helps |
This feature is called "const context" and was introduced in TypeScript 3.4 (March 2019, #29510), and support for JSDoc was added in TypeScript 4.5 (November 2021, #45464). But this only works for literals, not for imported values. Having imported values from JSON behave as with |
@MartinJohns as far as i understood your referencing to a proposal to import json as const i want to drive all values even json as const see my example frank-dspeed/frank-dspeed#10 |
That is not supported, and not really feasible. At the point where you want to use |
@MartinJohns thats why i wrote it in the proposal the direction does not matter but we need some more scope in future typescript versions to address a lot of small issues. or maybe we can let it lookup types the ide does that for imports all the time. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
The ability to support using explicit types for exact array lengths with JSON data.
π Search Terms
json exact matching array types
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
The ability to treat JSON arrays as exact arrays.
π Motivating Example
π» Use Cases
This allows making more-specific rather than less-specific types. In the above example, the compiler will flag the declaration because a
(number|string)[]
does not match the specific type I provided.The text was updated successfully, but these errors were encountered: