UNPKG

1.07 kBTypeScriptView Raw
1/**
2Returns a boolean for whether the given type is `never`.
3
4@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
5@link https://stackoverflow.com/a/53984913/10292952
6@link https://www.zhenghao.io/posts/ts-never
7
8Useful in type utilities, such as checking if something does not occur.
9
10@example
11```
12import type {IsNever, And} from 'type-fest';
13
14// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
15type AreStringsEqual<A extends string, B extends string> =
16 And<
17 IsNever<Exclude<A, B>> extends true ? true : false,
18 IsNever<Exclude<B, A>> extends true ? true : false
19 >;
20
21type EndIfEqual<I extends string, O extends string> =
22 AreStringsEqual<I, O> extends true
23 ? never
24 : void;
25
26function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
27 if (input === output) {
28 process.exit(0);
29 }
30}
31
32endIfEqual('abc', 'abc');
33//=> never
34
35endIfEqual('abc', '123');
36//=> void
37```
38
39@category Type Guard
40@category Utilities
41*/
42export type IsNever<T> = [T] extends [never] ? true : false;