mirror of
https://github.com/sindresorhus/type-fest.git
synced 2025-12-08 19:25:05 +00:00
50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
/**
|
|
Returns a boolean for whether the given type is `never`.
|
|
|
|
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
@link https://stackoverflow.com/a/53984913/10292952
|
|
@link https://www.zhenghao.io/posts/ts-never
|
|
|
|
Useful in type utilities, such as checking if something does not occur.
|
|
|
|
@example
|
|
```
|
|
import type {IsNever} from 'type-fest';
|
|
|
|
type And<A, B> =
|
|
A extends true
|
|
? B extends true
|
|
? true
|
|
: false
|
|
: false;
|
|
|
|
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
type AreStringsEqual<A extends string, B extends string> =
|
|
And<
|
|
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
IsNever<Exclude<B, A>> extends true ? true : false
|
|
>;
|
|
|
|
type EndIfEqual<I extends string, O extends string> =
|
|
AreStringsEqual<I, O> extends true
|
|
? never
|
|
: void;
|
|
|
|
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
if (input === output) {
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
endIfEqual('abc', 'abc');
|
|
//=> never
|
|
|
|
endIfEqual('abc', '123');
|
|
//=> void
|
|
```
|
|
|
|
@category Type Guard
|
|
@category Utilities
|
|
*/
|
|
export type IsNever<T> = [T] extends [never] ? true : false;
|