Home > Net >  Custom IsAny and IsUnknown generic based on type assignability(compatibility) table
Custom IsAny and IsUnknown generic based on type assignability(compatibility) table

Time:03-27

I tried to create IsAny generic based on this.

And my 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;

CodePudding user response:

The issue is that your isAny<any> yields boolean, which means both of these assignments will pass:

const testIsAny1: IsAny<any> = true;
const testIsAny2: IsAny<any> = false;

For the other isAny definition, testIsAny2 fails.

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;

TypeScript playground

CodePudding user response:

The problem is actually with IsAny.

You've noted that true: IsAny<any>, however, false: IsAny<any> also!

You've touched on an aspect of conditional types that can be rather counterintuitive.

I'll give this example:

type Extends<T, U> = T extends U ? true : false;

We have:

Extends<'a', 'a' | 'b'> =:= true
Extends<'c', 'a' | 'b'> =:= false

Nothing surprising so far.

What might be surprising is this:

Extends<'a' | 'c', 'a' | 'b'> =:= boolean

When we have T extends U ? …, if there is subtype of T that is a subtype of U and a subtype of T that is not a subtype of U, then the resulting type is the union of both branches.

  • Related