Repository navigation
Support exact matching array types from JSON filesΒ #49194
Description
Activity
MartinJohns commented
on May 20, 2022 ContributorMore actionsDuplicate of #32063.
Reacted by Andrew BranchPythonCoderAS commented
on May 20, 2022 AuthorMore actionsDuplicate 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
[1, 2]->1 | 2). I want something like[1, 2]to matchnumber[]and[number, number].MartinJohns commented
on May 21, 2022 ContributorMore actionsThe 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.the solution is simple we got casting to const since typescript 4.5 or 4.7 not sure
typescriptconst 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
MartinJohns commented
on May 21, 2022 ContributorMore actionsthe 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
constis exactly what #32063 is about.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 commented
on May 21, 2022 ContributorMore actionsThat 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.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.
- addedDuplicateAn existing issue was already createdAn existing issue was already created
on Jun 1, 2022 typescript-bot commented
on Jun 3, 2022 ContributorMore actionsThis issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
- locked as resolved and limited conversation to collaborators
on Oct 22, 2025
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
{"example": [1, 2, "three", 4]}π» 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.