Quantcast
Channel: Is there a way to prevent union types in TypeScript? - Stack Overflow
Browsing latest articles
Browse All 4 View Live

Answer by JohnLock for Is there a way to prevent union types in TypeScript?

This also works:type NoUnion<T, U = T> = T extends U ? [U] extends [T] ? T : never : never;

View Article



Answer by jcalz for Is there a way to prevent union types in TypeScript?

By the way, the "simpler" one I was trying to come up with looks like this:( NOTE: the following doesn't work in TS after v3.3, due to microsoft/TypeScript#34504:// type NotAUnion<T> = [T]...

View Article

Answer by Titian Cernicova-Dragomir for Is there a way to prevent union types...

I am unsure what the usecase for this is, but we can force the NoUnion to never if the passed type is a union type.As other mentioned conditional types distribute over a union, this is called...

View Article

Is there a way to prevent union types in TypeScript?

I'm new to conditional types, so I tried the most obvious static way, no success:type NoUnion<Key> = Key extends 'a' ? 'a' : Key extends 'b' ? 'b' : never;type B = NoUnion<'a'|'b'>;The B...

View Article
Browsing latest articles
Browse All 4 View Live




Latest Images