I tried to create
IsAny generic based on this.
IsAny generic seems to work fine.
But when I use it in another generic(
IsUnknown) it is broken:
const testIsUnknown2: IsUnknown<any> = true; // problem here, must be false
But when I change
IsAny generic to commented one, – it works fine again. So why is it happening? Because I do not see any difference in both
IsAny generics, but it seems my generic is not working inside other generics.
// type IsAny<T> = 0 extends (1 & T) ? true : false; type IsAny<T> = unknown extends T ? (T extends object ? true : false) : false; const testIsAny1: IsAny<any> = true; const testIsAny2: IsAny<unknown> = false; const testIsAny3: IsAny<string> = false; const testIsAny4: IsAny<object> = false; // unknown is only assignable to two types: unknown and any type IsUnknown<T> = unknown extends T ? (IsAny<T> extends true ? false : true) : false; const testIsUnknown1: IsUnknown<unknown> = true; const testIsUnknown2: IsUnknown<any> = true; // problem here, must be false const testIsUnknown3: IsUnknown<object> = false; const testIsUnknown4: IsUnknown<number> = false;
The issue is that your
boolean, which means both of these assignments will pass:
const testIsAny1: IsAny<any> = true; const testIsAny2: IsAny<any> = false;
For the other
Regarding your definition of
isAny, it fails because the conditional type
T extends object ? true : false returns both alternatives for
any. I couldn’t find any documentation about this, but you can fix it the same way you can prevent conditional types from distributing over unions, by surrounding both sides with square brackets (docs):
type IsAny<T> = unknown extends T ? ([T] extends [object] ? true : false) : false;
Answered By – Oblosys
Answer Checked By – Terry (BugsFixing Volunteer)