I attempted to craft a generic called IsAny
based on this resource.
The IsAny
generic appears to be functioning correctly.
However, when I implement it within another generic (IsUnknown
), it fails:
const testIsUnknown2: IsUnknown<any> = true; // issue here, should be false
Interestingly, switching out the IsAny
generic with the commented one resolves the problem. Why is this occurring? I can't distinguish any distinction between both versions of the IsAny
generic, yet it seems that my implementation does not work within 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; // issue here, should be false
const testIsUnknown3: IsUnknown<object> = false;
const testIsUnknown4: IsUnknown<number> = false;