question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infer a tuple type instead of array type when possible

See original GitHub issue

Suggestion

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

This is related to https://github.com/microsoft/TypeScript/issues/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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
RyanCavanaughcommented, May 27, 2021

Related #32758

1reaction
echentwcommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Tuples in TypeScript (Type Inference) - Stack Overflow
For arrays literals type inference does not infer tuples, it infers arrays, so ... In your case you can specify the type parameter:...
Read more >
Asking : could I infer tuple from array when passed as ... - Reddit
In my opinion, there is probably a way to infer the array as a tuple, with conditional generics or ... You can also...
Read more >
Handle literal arrays/tuples types in TypeScript
I have used empty array instead of never , because we want to filter an array, not to get either Head or Tail....
Read more >
Typing Arrays • Tackling TypeScript - Exploring JS
First, the inferred type is as narrow as possible. That causes an issue for let -declared variables: We cannot assign any tuple other...
Read more >
Documentation - TypeScript 4.0
Consider a function in JavaScript called concat that takes two array or tuple types and concatenates them together to make a new array....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found