microsoft / microsoft/TypeScript

Generic Type Alias Overloading

Open
#39,526 2 comments 0 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

  • variadic type alias
  • Generic variadic Type Alias
  • Generic Overload
  • Generic Type Alias Overloading

Suggestion

Ability to either:

  • overload generic type definitions
    or
  • define variadic type parameters for generic type aliases. (not sure how this would work)

Use Cases

My specific, immediate need is to create a generic variable number of input type alias for Intersection. I could see other use cases though, and imagine others would find it useful.

The closest work around is if I define default type parameters, like:

type Intersection<A, B, C = {} , D = {}, E = {}, F = {}> = A & B & C & D & E & F;

// and use like
let foo: Intersection<{ prop1: number}, {prop2: string}>;

// it evaluates to
{ prop1: number } &
{ prop2: number } &
{ } &
{ } &
{ } &
{ }

Examples

With overloads, the above type could be declared like:

type Intersection<A , B> = A & B;
type Intersection<A , B , C> = A & B & C;
type Intersection<A , B , C , D> = A & B & C & D;
type Intersection<A , B , C , D , E> = A & B & C & D & E;
type Intersection<A , B , C , D , E , F > = A & B & C & D & E & F;

I am not sure of a good syntax for the variadic type, it also seems like it would have a more limited use.

Checklist

My suggestion meets these guidelines: ( I think )

  • 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

Review the proposed generic type alias overloads and the alternative variadic type parameter idea in the issue examples. Determine the intended type-checking behavior, syntax, compatibility implications, and scope before identifying implementation and test entry points in the TypeScript compiler.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.