You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I built a tiny library that makes mixins typesafe, including detecting method collisions at compile time. The implementation is really simple, and it's a nicer API than the one in the TS handbook, I think.
I'm having trouble extending it to support more than a few parameters passed to mixin(A, B, C), because I have to test all permutations for Extract to work:
2-ary mixin():
Extract<keyof A, keyof B>
3-ary mixin():
Extract<keyof A, keyof B | keyof C>
Extract<keyof A | keyof C, keyof B>
4-ary mixin():
Extract<keyof A, keyof B | keyof C | keyof D>
Extract<keyof A | keyof D, keyof B | keyof C>
Extract<keyof A | keyof C, keyof B | keyof D>
Extract<keyof A | keyof C | keyof D, keyof B | keyof D>
This quickly explodes and gets really branchy, making the PreventCollisions type (below) really unwieldy.
Any suggestions for how to avoid this branchieness, to support more params? The thing I'd like to express is "What are the keys that shapes A, B, C ... Z have in common?"
The issue tracker is not the best place for questions like these, but I thought TS maintainers would be interested to see this solution. Feel free to close if you feel otherwise :)
The key is the & { "0": any } part. It tricks typescript into inferring a tuple type so that you have access to the elements passed via their numeric literal index. Discovered it in #22679. Never got an answer on if it's something that can be depended on to work in the future, but it has worked since 2.0 if not earlier so at least it's been pretty stable.
One thing is you'd be able to use rest parameters instead of manually passing an array. Besides that I don't know for sure since they aren't implemented yet and most of the discussion about them in #5453 happened before conditional types or even mapped types existed, but I'm hoping they'll be similarly flexible to work with.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
jack-williams commentedon Apr 14, 2018
Not sure about this.. but maybe:
kpdonn commentedon Apr 15, 2018
I think you need variadic types to do this right, but I've experimented with the same idea and a hacky way to accomplish something very similar is:
Playground link
The key is the
& { "0": any }part. It tricks typescript into inferring a tuple type so that you have access to the elements passed via their numeric literal index. Discovered it in #22679. Never got an answer on if it's something that can be depended on to work in the future, but it has worked since 2.0 if not earlier so at least it's been pretty stable.bcherny commentedon Apr 22, 2018
Kevin Donnelly (@kpdonn) Man, that is some crazy type level programming. What would this look like with variadic types?
kpdonn commentedon Apr 23, 2018
One thing is you'd be able to use rest parameters instead of manually passing an array. Besides that I don't know for sure since they aren't implemented yet and most of the discussion about them in #5453 happened before conditional types or even mapped types existed, but I'm hoping they'll be similarly flexible to work with.
typescript-bot commentedon May 8, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.