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 ArticleAnswer 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 ArticleAnswer 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 ArticleIs 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
More Pages to Explore .....