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

proposal:rest infer #26246

Closed
4 tasks done
kgtkr opened this issue Aug 7, 2018 · 5 comments
Closed
4 tasks done

proposal:rest infer #26246

kgtkr opened this issue Aug 7, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@kgtkr
Copy link

kgtkr commented Aug 7, 2018

Search Terms

Generics

Suggestion

Decomposition of tuple type

Examples

type Hoge<T extends any[]> = T extends [infer X, ...infer XS] ? X | Hoge<XS> : never;

There is a problem of #14174

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)
@JakeTunaley
Copy link

JakeTunaley commented Aug 7, 2018

For anyone who needs a workaround for this today, you can use function types and TypeScript 3.0 rest tuples:

type Tail<T extends any[]> = ((...args: T) => any) extends ((head: any, ...tail: infer U) => any) ? U : never;

@kgtkr
Copy link
Author

kgtkr commented Aug 7, 2018

I did not know how to solve it

Is this correct?

type ToObj<T extends any[]> = ((...args: T) => any) extends ((head: infer X, ...tail: infer XS) => any) ? { x: X, xs: ToObj<XS> } : [];
type Foo = ToObj<[1, 2, 3, 4, 5]>;//{x: 1;xs: any;}

@AlCalzone
Copy link
Contributor

AlCalzone commented Aug 7, 2018

Duplicate of #25719 ?

Or are you trying to create a union of all items? Then it's just

type UnionOf<T extends any[]> = T[number];

type Foo = UnionOf<[1,2,3]>; // 1 | 2 | 3

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs More Info The issue still hasn't been fully clarified labels Aug 8, 2018
@RyanCavanaugh
Copy link
Member

It's not clear what problem you're trying to solve here. The suggestion template has many more fields and it'd be great if you could fill them in completely -- just showing some syntax you'd expect to work doesn't give us enough context on what you'd like to happen, how you'd expect it to work, or reasons why this would be a good addition to the language. Thanks!

@kgtkr
Copy link
Author

kgtkr commented Aug 8, 2018

Close this issue because it is a duplicate

@kgtkr kgtkr closed this as completed Aug 8, 2018
@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Needs More Info The issue still hasn't been fully clarified Suggestion An idea for TypeScript labels Aug 8, 2018
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