I've noticed the library currently lacks a IsAssignable<T, U> utility type for checking assignment compatibility between types, which is a common task in TypeScript.
typeIsAssignable<A,B>=[A]extends[B] ? true : false;
This utility works similar to type Extends<A, B> = A extends B ? true : false, but it never evaluates to never or boolean and works better with unions and intersections.
typeA=IsAssignable<string,string>;// ^? truetypeB=IsAssignable<'foo',string>;// ^? truetypeC=IsAssignable<never,never>;// Only `never` is assignable to `never`// ^? truetypeD=IsAssignable<1|2,1>;// Union type is correctly handled// ^? falsetypeE=IsAssignable<1&2,never>;// Only `never` is assignable to `never`// ^? truetypeF=IsAssignable<string&number,never>;// Only `never` is assignable to `never`// ^? truetypeG=IsAssignable<any,{}>;// `any` is assignable to all types// ^? truetypeH=IsAssignable<{},any>;// `any` is assignable to all types// ^? truetypeI=IsAssignable<any,1>;// `any` is assignable to all types// ^? truetypeJ=IsAssignable<never,1>;// `never` is assignable to all types// ^? truetypeK=IsAssignable<any,never>;// Only `never` is assignable to `never`// ^? falsetypeL=IsAssignable<never,any>;// `never` is assignable to all types// ^? truetypeM=IsAssignable<never,{}>;// `never` is assignable to all types// ^? truetypeN=IsAssignable<boolean,true>;// Union type is correctly handled// ^? falseI'm not entirely certain if this type will work in more complex edge cases, but the scenarios listed here should cover a wide range of common cases.
I've noticed the library currently lacks a
IsAssignable<T, U>utility type for checking assignment compatibility between types, which is a common task in TypeScript.This utility works similar to
type Extends<A, B> = A extends B ? true : false, but it never evaluates toneverorbooleanand works better with unions and intersections.I'm not entirely certain if this type will work in more complex edge cases, but the scenarios listed here should cover a wide range of common cases.