Reputation: 6844
In the following code I want type A
to be true
, and not the union of both:
type isFalseAndNotAny<T> = T extends false ? 'false' : 'true';
// how do I get here only `true`?
type A = isFalseAndNotAny<any>;
// that's OK we get only `false`
type B = isFalseAndNotAny<false>;
Is there any way in Typescript to do something similar like in Javascript we use the triple equality ===
operator?
Upvotes: 1
Views: 382
Reputation: 33041
You need slightly modify your util.
// credits goes to https://stackoverflow.com/questions/55541275/typescript-check-for-the-any-type
type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N;
type IsAny<T> = IfAny<T, true, false>;
type isFalseAndNotAny<T> = T extends false ? IsAny<T> extends false ? 'false' : 'true' : 'true';
// true
type A = isFalseAndNotAny<any>;
// false
type B = isFalseAndNotAny<false>;
You need explicitly check if T
extends false
and not any
, it is like T===false && T!==any
Upvotes: 1