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.
type IsAssignable<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.
type A = IsAssignable<string, string>;
// ^? true
type B = IsAssignable<'foo', string>;
// ^? true
type C = IsAssignable<never, never>; // Only `never` is assignable to `never`
// ^? true
type D = IsAssignable<1 | 2, 1>; // Union type is correctly handled
// ^? false
type E = IsAssignable<1 & 2, never>; // Only `never` is assignable to `never`
// ^? true
type F = IsAssignable<string & number, never>; // Only `never` is assignable to `never`
// ^? true
type G = IsAssignable<any, {}>; // `any` is assignable to all types
// ^? true
type H = IsAssignable<{}, any>; // `any` is assignable to all types
// ^? true
type I = IsAssignable<any, 1>; // `any` is assignable to all types
// ^? true
type J = IsAssignable<never, 1>; // `never` is assignable to all types
// ^? true
type K = IsAssignable<any, never>; // Only `never` is assignable to `never`
// ^? false
type L = IsAssignable<never, any>; // `never` is assignable to all types
// ^? true
type M = IsAssignable<never, {}>; // `never` is assignable to all types
// ^? true
type N = IsAssignable<boolean, true>; // Union type is correctly handled
// ^? false
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.
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.