Skip to content
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

Closed
5 tasks done
PythonCoderAS opened this issue May 20, 2022 · 9 comments
Closed
5 tasks done

Support exact matching array types from JSON files #49194

PythonCoderAS opened this issue May 20, 2022 · 9 comments
Labels
Duplicate An existing issue was already created

Comments

@PythonCoderAS
Copy link

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.

@MartinJohns
Copy link
Contributor

Duplicate of #32063.

@PythonCoderAS
Copy link
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].

@MartinJohns
Copy link
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.

@frank-dspeed
Copy link

frank-dspeed commented May 21, 2022

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

@MartinJohns
Copy link
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.

@frank-dspeed
Copy link

@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
Copy link
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.

@frank-dspeed
Copy link

@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.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Jun 1, 2022
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants