UNPKG

2.9 kBTypeScriptView Raw
1/**
2 * Primitive
3 * @desc Type representing [`Primitive`](https://developer.mozilla.org/en-US/docs/Glossary/Primitive) types in TypeScript: `string | number | bigint | boolean | symbol | null | undefined`
4 * @example
5 * type Various = number | string | object;
6 *
7 * // Expect: object
8 * type Cleaned = Exclude<Various, Primitive>
9 */
10export declare type Primitive = string | number | bigint | boolean | symbol | null | undefined;
11/**
12 * Falsy
13 * @desc Type representing falsy values in TypeScript: `false | "" | 0 | null | undefined`
14 * @example
15 * type Various = 'a' | 'b' | undefined | false;
16 *
17 * // Expect: "a" | "b"
18 * Exclude<Various, Falsy>;
19 */
20export declare type Falsy = false | '' | 0 | null | undefined;
21/**
22 * Nullish
23 * @desc Type representing [nullish values][https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing] in TypeScript: `null | undefined`
24 * @example
25 * type Various = 'a' | 'b' | undefined;
26 *
27 * // Expect: "a" | "b"
28 * Exclude<Various, Nullish>;
29 */
30export declare type Nullish = null | undefined;
31/**
32 * Tests for one of the [`Primitive`](https://developer.mozilla.org/en-US/docs/Glossary/Primitive) types using the JavaScript [`typeof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) operator
33 *
34 * Clarification: TypeScript overloads this operator to produce TypeScript types if used in context of types.
35 *
36 * @param val The value to be tested
37 * @returns If `val` is primitive. If used in the flow of the program typescript will infer type-information from this.
38 *
39 * @example
40 * const consumer = (value: Primitive | Primitive[]) => {
41 * if (isPrimitive(value)) {
42 * return console.log('Primitive value: ', value);
43 * }
44 * // type of value now inferred as Primitive[]
45 * value.map((primitive) => consumer(primitive));
46 * };
47 */
48export declare const isPrimitive: (val: unknown) => val is Primitive;
49/**
50 * Tests for Falsy by simply applying negation `!` to the tested `val`.
51 *
52 * The value is mostly in added type-information and explicity,
53 * but in case of this simple type much the same can often be archived by just using negation `!`:
54 * @example
55 * const consumer = (value: boolean | Falsy) => {
56 * if (!value) {
57 * return ;
58 * }
59 * type newType = typeof value; // === true
60 * // do stuff
61 * };
62 */
63export declare const isFalsy: (val: unknown) => val is Falsy;
64/**
65 * Tests for Nullish by simply comparing `val` for equality with `null`.
66 * @example
67 * const consumer = (param: Nullish | string): string => {
68 * if (isNullish(param)) {
69 * // typeof param === Nullish
70 * return String(param) + ' was Nullish';
71 * }
72 * // typeof param === string
73 * return param.toString();
74 * };
75 */
76export declare const isNullish: (val: unknown) => val is null | undefined;