-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Add IsAssignable
Utility Type
#3
Comments
I'd welcome a PR on this, but would you mind sharing a real-world use-case? |
oh wait, that's not a type... |
It is more reliable than import type { ArrayIndices, IntRange } from "type-fest";
import type { Expect, ExpectFalse, Extends } from "type-testing";
type IsAssignable<A, B> = [A] extends [B] ? true : false;
const values = ['a', 'b', 'c'] as const;
type ValueIndices = ArrayIndices<typeof values>;
type Cases_using_Extends = [
Expect<Extends<IntRange<0, 2>, ValueIndices>>,
Expect<Extends<IntRange<0, 3>, ValueIndices>>,
ExpectFalse<Extends<IntRange<0, 4>, ValueIndices>>, // Error: `boolean` is not assignable to `false`
];
type Cases_using_IsAssignable = [
Expect<IsAssignable<IntRange<0, 2>, ValueIndices>>,
Expect<IsAssignable<IntRange<0, 3>, ValueIndices>>,
ExpectFalse<IsAssignable<IntRange<0, 4>, ValueIndices>>, // OK
] Try it on TS Playground. It also avoids some of the strange behavior of |
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 tonever
orboolean
and 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.
The text was updated successfully, but these errors were encountered: