UNPKG

880 BTypeScriptView Raw
1// Can eventually be replaced with the built-in once this library supports
2// TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
3type NoInfer<T> = T extends infer U ? U : never;
4
5/**
6Returns a boolean for whether the given type is `any`.
7
8@link https://stackoverflow.com/a/49928360/1490091
9
10Useful in type utilities, such as disallowing `any`s to be passed to a function.
11
12@example
13```
14import type {IsAny} from 'type-fest';
15
16const typedObject = {a: 1, b: 2} as const;
17const anyObject: any = {a: 1, b: 2};
18
19function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
20 return obj[key];
21}
22
23const typedA = get(typedObject, 'a');
24//=> 1
25
26const anyA = get(anyObject, 'a');
27//=> any
28```
29
30@category Type Guard
31@category Utilities
32*/
33export type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;