microsoft / microsoft/TypeScript

Spreading tuple into generic/type arguments

Open
#39,648 8 comments 37 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

Search Terms

  • spreading generic parameters
  • generic parameter spread
  • tuple generic spread
  • spreading generic argument
  • spreading typeArgument
  • spreading type argument

Suggestion

Allow spreading of a tuple type into type arguments.

The idea is that a tuple of n elements gets spread into generic arguments with r arguments. If n is larger than r, only the first r items are used. If n is smaller than r it is possible to add elements after the spread argument. E.g.

function foo<A, B, C, D>() {
}

type ThreeType = [Foo, Bar, Baz];

foo<...ThreeType, Delta>();

Use Cases

I find myself repeating complex generic types. Ideally I could shorten lengthy repetition into a single type.

Examples

dialog.open takes three generic arguments, these have to be repeated each call. This makes the call less legible and reduces DRYness.

const dialogRef = this.dialog.open<
    PrinterSelectDialogComponent,
    PrinterSelectDialogData,
    OfferProductPrinter>(
        PrinterSelectDialogComponent,
        {
            data: {
                printer,
            },
        },
    );

After this proposal

export type PrinterSelectDialog = [
    PrinterSelectDialogComponent,
    PrinterSelectDialogData,
    OfferProductPrinter,
];


const dialogRef = this.dialog.open<...PrinterSelectDialog>(PrinterSelectDialogComponent, {
    data: {
        service,
    },
});

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.

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

Start by reviewing the proposal and its examples for the intended generic-argument spreading behavior. Done means supporting tuple spreads in type arguments, including truncation when the tuple is longer and trailing elements when it is shorter, without changing emitted JavaScript.

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
Active
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.