Skip to content

Support exact matching array types from JSON filesΒ #49194

Description

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:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

The ability to treat JSON arrays as exact arrays.

πŸ“ƒ Motivating Example

{"example": [1, 2, "three", 4]}
import thing from 'test.json';

interface Test {
    example: [number, number, string, number];
}

export const example: Test = thing;

πŸ’» 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.

Activity

  1. MartinJohns commented on May 20, 2022

    @MartinJohns
    Contributor

    Duplicate of #32063.

  2. PythonCoderAS commented on May 20, 2022

    @PythonCoderAS
    Author

    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 [1, 2] -> 1 | 2). I want something like [1, 2] to match number[] and [number, number].

  3. MartinJohns commented on May 21, 2022

    @MartinJohns
    Contributor

    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.

  4. frank-dspeed commented on May 21, 2022

    @frank-dspeed

    the solution is simple we got casting to const since typescript 4.5 or 4.7 not sure
    typescript

    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

  5. MartinJohns commented on May 21, 2022

    @MartinJohns
    Contributor

    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.

  6. frank-dspeed commented on May 21, 2022

    @frank-dspeed

    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

  7. MartinJohns commented on May 21, 2022

    @MartinJohns
    Contributor

    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.

  8. frank-dspeed commented on May 21, 2022

    @frank-dspeed

    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.

  9. typescript-bot commented on Jun 3, 2022

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  10. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions