microsoft / microsoft/TypeScript

Tuple iteration and merging

Open
#26,058 14 comments 24 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Edit: Remove indexing part
Edit 2: Replace tuple mapping syntax with normal mapped types

Search Terms

Concatentate and merge tuples' entries.

Suggestion

  1. I would like to be able to extract and spread in positions other than the last part of a tuple.
  2. I would like to merge a tuple into an intersection or concatenation of its members.

Use Cases

  • Converting a tuple to its intersection, such as with Object.assign's assignee.
  • Typing Array.prototype.concat correctly for tuples.

Syntax

The syntax comes in a few new forms:

  • [...A, B] appends B to the tuple A. Similarly, [...infer A, any] extends B ? A : [] drops the last item of the tuple and [...any[], infer A] extends B ? A : never extracts the first.
  • {... ...T} for an n-ary merge and [... ...T] an n-ary concatenation.

Examples

Here's the types for each method I mentioned above.

interface Array<T> {
    concat<U extends any[]>(...others: U): [...this, ... ...{[I in keyof U]: (
        U[I] extends any[] ? N : U[I] extends ArrayLike<infer R> ? R[] : [U[I]]
    )}];
}

interface ObjectConstructor {
    assign(target: {... ...typeof sources}, ...sources: object[]): typeof target;
}

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. new expression-level syntax)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue proposes tuple spreading, tuple concatenation, and tuple-to-intersection merging in TypeScript's type system, with examples for Array.concat and Object.assign. No implementation files, tests, or entry points are named; determine the relevant type-system area first, then establish tests showing the proposed tuple behavior and examples work.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.