You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support exact matching array types from JSON filesΒ #49194
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.
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 [1, 2] -> 1 | 2). I want something like [1, 2] to match number[] and [number, number].
The type can't be number[] and [1, 2] at the same time. The one is a mutable array with an arbitrary amount of elements, the other is a readonly array with exactly two elements. These types contradict each other.
constnumberArray=[1,2];constliteralArray=/** @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
the solution is simple we got casting to const since typescript 4.5 or 4.7 not sure
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 const is exactly what #32063 is about.
That is not supported, and not really feasible. At the point where you want to use as const the compiler already lost the information what type it is. It can't deduce readonly [1, 2] from a value typed number[]. Only the other way is possible and could be achieved using a conditional type.
Martin Johns (@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.
Activity
MartinJohns commentedon May 20, 2022
Duplicate of #32063.
PythonCoderAS commentedon May 20, 2022
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
[1, 2]->1 | 2). I want something like[1, 2]to matchnumber[]and[number, number].MartinJohns commentedon May 21, 2022
The type can't be
number[]and[1, 2]at the same time. The one is a mutable array with an arbitrary amount of elements, the other is a readonly array with exactly two elements. These types contradict each other.frank-dspeed commentedon May 21, 2022
the solution is simple we got casting to const since typescript 4.5 or 4.7 not sure
typescript
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
it got implemented in a way so that you need to indicate that on assignment hope that helps
MartinJohns commentedon May 21, 2022
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
constis exactly what #32063 is about.frank-dspeed commentedon May 21, 2022
Martin Johns (@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
MartinJohns commentedon May 21, 2022
That is not supported, and not really feasible. At the point where you want to use
as constthe compiler already lost the information what type it is. It can't deducereadonly [1, 2]from a value typednumber[]. Only the other way is possible and could be achieved using a conditional type.frank-dspeed commentedon May 21, 2022
Martin Johns (@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.
typescript-bot commentedon Jun 3, 2022
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.