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

Suggestion: add UnionToIntersection and Improve IntersectTuple #97

Closed
JoshRosenstein opened this issue Apr 11, 2019 · 3 comments
Closed

Comments

@JoshRosenstein
Copy link
Contributor

by adding the magical UnionToIntersection

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void)
  ? I
  : never;

Ref: https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type

Will give you a new feature and can replace :

export type IntersectTuple<T extends Vector<any>> =
    Length<T> extends 1 ? T[0] :
    Length<T> extends 2 ? T[0] & T[1] :
    Length<T> extends 3 ? T[0] & T[1] & T[2] :
    Length<T> extends 4 ? T[0] & T[1] & T[2] & T[3] :
    Length<T> extends 5 ? T[0] & T[1] & T[2] & T[3] & T[4] :
    Length<T> extends 6 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] :
    Length<T> extends 7 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] :
    Length<T> extends 8 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] :
    Length<T> extends 9 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] & T[8] :
    Length<T> extends 10 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] & T[8] & T[9] :
        any;

with:

export type IntersectTuple<T extends Vector<any>>=UnionToIntersection<T[number]>
@andnp
Copy link
Owner

andnp commented Apr 13, 2019

This would be great! I was hesitant to add this at first because I wanted to be sure it was stable. These magical types make me nervous that the Typescript compiler will make internal changes and break the unsupported magic.

This type seems stable enough to me. If you'd like to add it, feel free to put up a PR! Otherwise I'll hopefully get around to it this weekend. I'll definitely be able to get to it by next weekend after a few deadlines cool down.

@andnp
Copy link
Owner

andnp commented Apr 13, 2019

Oh also, I should note that you should avoid extends any. Re: the discussion in #88 :)

@JoshRosenstein
Copy link
Contributor Author

@andnp Will do! Also after seeing this issue and comment
microsoft/TypeScript#29594 (comment) and also seeing tsc uses this type- not worried about stability.

I'll submit a PR when i get a chance. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants