Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Type Alias Overloading #39526

Open
5 tasks done
erichiller opened this issue Jul 9, 2020 · 2 comments
Open
5 tasks done

Generic Type Alias Overloading #39526

erichiller opened this issue Jul 9, 2020 · 2 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@erichiller
Copy link

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.
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jul 10, 2020
@RyanCavanaugh
Copy link
Member

I absolutely know we had a couple meetings about this in the past, but can't find notes for it, so we'll talk about it again 🙃

@ecyrbe
Copy link

ecyrbe commented Mar 17, 2021

could i suggest using c++ like syntax for variadic templates? :

type intersection<T, U...> = T &... U;
type union<T, U...> = T |... U;
type template <T, U...> = `${T}$...{U}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants