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

Infer a tuple type instead of array type when possible #44309

Open
echentw opened this issue May 27, 2021 · 10 comments
Open

Infer a tuple type instead of array type when possible #44309

echentw opened this issue May 27, 2021 · 10 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@echentw
Copy link

echentw commented May 27, 2021

Suggestion

I'd prefer typescript to be more aggressive about inferring tuple types, instead of always defaulting to Array.

This is related to #6574. I'd like to open up a discussion about this again since that thread is locked, and conversation stopped after a workaround was provided without completely addressing the general issue.

🔍 Search Terms

Typescript, tuple, tuples, map, inference

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

Existing typescript code could start emitting type errors from this change, so it would make sense to provide this as an option in tsconfig.

⭐ Suggestion

Typescript should infer a type to be a tuple instead of an array when it's possible.

📃 Motivating Example

The specific example I have in mind is for Array.prototype.map(), but there are probably more.

const tuple: [number, number, number] = [1, 2, 3];
const result = tuple.map(value => value + 1);

Here, typescript infers the type of result to be number[] rather than [number, number, number]. This could result in some wonkiness when trying to access values inside the tuple:

const [a, b, c, d] = result; // Typescript doesn't detect an error since result is an Array type.
const e = result[10]; // Typescript doesn't detect an error since result is an Array type.

Both of these errors can be caught if Typescript instead inferred result to be the tuple type [number, number, number].

💻 Use Cases

The above example is a use case. I want typescript to tell me when I make errors in my code.

@RyanCavanaugh
Copy link
Member

Related #32758

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels May 27, 2021
@RyanCavanaugh
Copy link
Member

Typescript should infer a type to be a tuple instead of an array when it's possible.

What's your rough definition of "possible" ?

@echentw
Copy link
Author

echentw commented May 27, 2021

@RyanCavanaugh I don't have a strict definition. But for me, Array.prototype.map() is a very clear example where Typescript should be able to infer a tuple type, since map() preserves the length of the input.

Another one that comes to mind is x.concat(y), if x and y are both tuple types. If you'd like me to compile a list of array methods where I think this would useful, I'd be happy to do that.

I'm more curious about why this isn't a feature in Typescript yet, and whether this is something that we can/should introduce.

@RyanCavanaugh
Copy link
Member

I'm more curious about why this isn't a feature in Typescript yet,

I'm not sure how to answer that; we have thousands of open suggestions, all which were opened by people who think they are good ideas and would like to have them 🙂

@RyanCavanaugh
Copy link
Member

I don't have a strict definition. But for me, Array.prototype.map() is a very clear example where Typescript should be able to infer a tuple type, since map() preserves the length of the input.

So for TypeScript, map, concat, and pretty much everything else are totally indistinguishable from any other method on any other object you could call. We'd need some better way of understanding what this means.

As an example of why this just isn't the default already, I wouldn't expect this code to be an error:

const p = [1, 2, 3].map(n => -n);
p.push(-4);

but it sounds like you're proposing that it should be (since p would be a [number, number, number]).

@echentw
Copy link
Author

echentw commented May 27, 2021

Just for sake of exploration: that question is whether to have typescript infer the type of an array literal to be an Array or tuple. One way to go about it is to default to whatever is the most common usage. And for the other use case, people can always explicitly add the type in.

// An option if typescript infers the tuple type
const p: number[] = [1, 2, 3].map(n => -n);
p.push(-4);

Either way, I'm fine with typescript inferring the type of [1, 2, 3] to be number[] instead of [number, number, number], so the code snippet above wouldn't produce an error.

I would, however, want this code to produce an error:

const p = ([1, 2, 3] as [number, number, number]).map(n => -n);
p.push(-4);
console.log(p[3]); // added this line here since p.push(-4) wouldn't produce an error even if p were a tuple type

@echentw
Copy link
Author

echentw commented May 27, 2021

So for TypeScript, map, concat, and pretty much everything else are totally indistinguishable from any other method on any other object you could call. We'd need some better way of understanding what this means.

Hmm, if there's a hard technical limitation, then I would understand. I don't know anything about how Typescript is implemented underneath.

@whzx5byb
Copy link

It seems #11252 have fixed it, and it's status is "Merged". Is there any regression?

@echentw
Copy link
Author

echentw commented May 28, 2021

@whzx5byb seems like there may have been a regression.

const myTuple: [number, number] = [1, 2];

// result is inferred to be number[]
const result = myTuple.map(v => v + 1);

Playground link

@whzx5byb
Copy link

Some related issues:
#29841
#33832
#36554

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants