Skip to content

Support exact matching array types from JSON filesΒ #49194

Closed
@PythonCoderAS

Description

@PythonCoderAS

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.

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