I currently have this type:
type ReturnType<T, E> = [T, E]
I 'm trying to make this type "smarter" by these rules:
T is undefined, then the type would be [undefined, E]E is undefined, then the type would be [T]T and E can't both be undefinedI'm trying to apply these rules using Typescript's Conditional Types.
Is this even possible to implement?
This is what I got so far:
type ReturnType<T, E> =
T extends undefined
? E extends undefined ? never : [undefined, E]
: E extends undefined ? [T] : never
What you seem to be missing is that you can do tests for both T and E at the same time with:
[T, E] extends [TypeA, TypeB] ? ... : ...
Putting that to use you can do:
type MyReturnType<T, E> =
[T, E] extends [undefined, undefined] ? never
: T extends undefined ? [undefined, E]
: E extends undefined ? [T]
: [T, E]
type A = MyReturnType<undefined, undefined> // never
type B = MyReturnType<string, undefined> // [string]
type C = MyReturnType<undefined, number> // [undefined, number]
type D = MyReturnType<boolean, string> // [boolean, string]
Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With