Skip to content

Automatically type inference for tuple arrays #32391

@lffg

Description

@lffg

Search Terms

automatically type inference for tuple arrays

Suggestion

Currently, TypeScript is not able to automatically infer the type of an array of tuples.

For example:

const data = [
  ['name', 'Luiz Felipe', true],
  ['age', 16, false]
];

In the above example, TypeScript will infer the type of the data variable as:

(string | boolean)[]

But I think that it should have inferred to:

[string, string, boolean][]

I know that I could have defined the types explicitly in the variable declaration, but I suggest that TypeScript implements this automatic inference system in tuple arrays.

Use Cases

This lack of tuple arrays type inference really annoys me when I need to do something like that:

[
  ['accessToken', tokens.accessToken, ms('15m')],
  ['refreshToken', tokens.refreshToken, ms('90 days')]
].forEach(([key, val, maxAge]) => res.cookie(key, val, { maxAge }));

Because key, val and maxAge are incorrectly typed as string | number.

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions