-
Notifications
You must be signed in to change notification settings - Fork 118
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
Add NonUnion type #100
Comments
Rationale and code looks ok. Gratz for the additional research. @krzkaczor @macbem @lucifer1004 what do you think? Does this look useful, is the name intuitive enough? |
Looks great - IMO the name is clear enough and the type is quite useful. |
Great proposal 👍 Personally, I don't see anything wrong in: simpleFn(exampleA.foo, exampleB.complex); // WRONG USAGE, no error
That being said I can imagine that there are some good use cases for such type and if people are searching for such type they apparently found some. It just would be cool if we could improve somehow rationale and provide good usecase for such type in docs 🤔 |
Yes, but from a general "union type". The goal is to e.g. enforce passing properties coming from the same particular "discriminated union subtype". Usually I'd just pass |
@quezak okay i think now i understand it better. Yes, the name is quite bad but with good examples it will be fine i guess. Can we have good examples? :D Idk maybe renaming and implementing I think it was very similar with Buildable type - until good example appeared I was confused. |
I think @akwodkiewicz wanted to have a general name meaning "this is not a union type", but maybe we can think of a more specific one for the most common use case I see, meaning "this can be only one particular specific subtype of a union, not a few of them and not the whole union" |
Reworded example intro: if you have |
Summary
I would like to propose a
NonUnion
type:I've seen the concept in a couple of places [0] [1] and I've recently used it myself.
NonUnion<T>
type resolves toT
ifT
is not a union. Otherwise, it resolves tonever
.By making sure that
T
is a particular union element, you can use index types for generic function params.Example
Notes
NonUnion<T>
definition has to use[T]
tuple, due to how distributive conditional types [2] work (nakedT
would be distributed to several definitions ofT1 ? ... | T2 ? ... | ...
).References
[0] https://stackoverflow.com/a/50641073/7134149
[1] microsoft/TypeScript#32909
[2] https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types
The text was updated successfully, but these errors were encountered: