-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
types.ts
114 lines (86 loc) · 3.14 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Represents any plain function (no `this`)
*/
export type AnyFunction = (...args: any[]) => any;
export type UnknownFunction = (...args: unknown[]) => unknown;
/**
* Represents any plain object (or Record, as TypeScript calls it).
*
* Prefer {@link UnknownRecord} unless you're encountering issues with it.
*/
export type AnyRecord = Record<PropertyKey, any>;
/**
* Represents any plain object (or Record, as TypeScript calls it)
*
* Stricter than {@link AnyRecord}. Not all records can be assigned to this value due to how TypeScript works
* but its usage is recommended because the value won't be typed as any.
*/
export type UnknownRecord = Record<PropertyKey, unknown>;
export type Nullish = null | undefined;
export type NonNullish = {};
/**
* Makes the type accept null & undefined
*/
export type MakeNullish<T> = T | Nullish;
export type MakeNonNullish<T> = NonNullable<T>;
export type NonUndefined<T> = T extends undefined ? never : T;
export type NonNull<T> = T extends null ? never : T;
export type NonUndefinedKeys<T, K extends keyof T> = {
[P in keyof T]: P extends K ? NonUndefined<T[P]> : T[P];
};
export type AllowArray<T> = T | T[];
export type AllowIterable<T> = T | Iterable<T>;
export type AllowReadonlyArray<T> = T | readonly T[];
export type AllowPromise<T> = T | Promise<T>;
/**
* Like {@link Partial}, but also allows undefined.
* Useful when "exactOptionalPropertyTypes" is enabled.
*/
export type PartialOrUndefined<T> = {
[P in keyof T]?: T[P] | undefined;
};
/**
* Type helper for making certain fields of an object optional.
*/
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
export type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
export type StrictRequiredBy<T, K extends keyof T> = NonUndefinedKeys<
Omit<T, K> & Required<Pick<T, K>>,
K
>;
export type ReadOnlyRecord<K extends PropertyKey, V> = Readonly<Record<K, V>>;
export type PartialRecord<K extends PropertyKey, V> = {
[P in K]?: V;
};
export type PartialReadonlyRecord<K extends PropertyKey, V> = Readonly<PartialRecord<K, V>>;
export type Entry<Key, Value> = [key: Key, value: Value];
export type JsonArray = JsonValue[];
export type JsonObject = { [key: string]: JsonValue };
export type JsonPrimitive = string | number | boolean | null;
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
export type PickByType<T, U> = { [K in keyof T as U extends T[K] ? K : never]: T[K] };
export interface ReadonlyMapLike<K, V> {
entries(): IterableIterator<Entry<K, V>>;
get(key: K): V | undefined;
has(key: K): boolean;
keys(): IterableIterator<K>;
readonly size: number;
[Symbol.iterator](): IterableIterator<Entry<K, V>>;
values(): IterableIterator<V>;
}
export interface MapLike<K, V> extends ReadonlyMapLike<K, V> {
clear(): void;
delete(key: K): boolean;
set(key: K, value: V): this;
}
export interface ReadonlySetLike<V> {
has(value: V): boolean;
readonly size: number;
[Symbol.iterator](): IterableIterator<V>;
values(): IterableIterator<V>;
}
export interface SetLike<V> extends ReadonlySetLike<V> {
add(value: V): this;
clear(): void;
delete(value: V): boolean;
}