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 VariadicOperation type helper (Higher-Kind types) #60325

Closed
6 tasks done
vladmw5 opened this issue Oct 22, 2024 · 3 comments
Closed
6 tasks done

Generic VariadicOperation type helper (Higher-Kind types) #60325

vladmw5 opened this issue Oct 22, 2024 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@vladmw5
Copy link

vladmw5 commented Oct 22, 2024

πŸ” Search Terms

type helpers as type parameters, recursive types, variadic type

βœ… Viability Checklist

⭐ Suggestion

Consider the following code sample:

type Intersection<L, R> = L & R;

I was not satisfied with the limitation of being able to pass only 2 operands, so I created a recursive type MultipleIntersection:

type MultipleIntersection<A extends any[]> = A extends [infer F, ...infer R extends any[]]
  ? Intersection<F, R extends [infer L] ? L : MultipleIntersection<R>>
  : never;

type x = {
  a: string;
}

type y = {
  b: number;
}

type z = {
  c: boolean;
}

type w = {
  d: string[];
}

type testCortege = [x, y, z, w];

type test = MultipleIntersection<testCortege> // x & y & z & w

// Basically my MultipleIntersection type does the following:
type validTest = Intersection<testCortege[0], Intersection<testCortege[1], Intersection<testCortege[2], testCortege[3]>>>

Generally speaking, in the implementation of the MultipleIntersection type, if I replace the Intersection type helper with any other binary type helper, I would get the "Multiple" (or n-ary, aka variadic) version of that type.

My question is: is it possible (is there any TS syntax for that) to create a generic VariadicOperation<A extends any[], O extends ???what> type that would allow to pass a binary type-helper?

full code example

πŸ“ƒ Motivating Example

Typescript is finally Turing complete with this feature!!!

πŸ’» Use Cases

  1. To be able to create generic and reusable type helpers
  2. No TS syntax supports that behavior
  3. I don't know any workarounds at the moment
@vladmw5 vladmw5 changed the title Generic VariadicOperation type helper Generic VariadicOperation type helper (Higher-Kind types) Oct 22, 2024
@xiBread
Copy link

xiBread commented Oct 22, 2024

#1213

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 31, 2024
@RyanCavanaugh
Copy link
Member

Typescript is finally Turing complete with this feature!!!

This has been true for a long time, for better or worse πŸ₯΅

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants