-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.ts
85 lines (77 loc) · 2.27 KB
/
index.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
import type { Optional } from '..';
import { getComplexKeys } from './format';
export * from './array';
export * from './check';
export * from './class-to-invokable';
export * from './dialect';
export * from './format';
export * from './join-sql-fragments';
export * from './object';
export * from './sequelize-method';
export * from './string';
export * from './dayjs';
/**
* getComplexSize
*
* @param obj
* @returns Length of object properties including operators if obj is array returns its length
* @private
*/
export function getComplexSize(obj: object | any[]): number {
return Array.isArray(obj) ? obj.length : getComplexKeys(obj).length;
}
export type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
export type PartlyRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
export type AnyFunction = (...args: any[]) => any;
/**
* Returns all shallow properties that accept `undefined` or `null`.
* Does not include Optional properties, only `undefined` or `null`.
*
* @example
* ```typescript
* type UndefinedProps = NullishPropertiesOf<{
* id: number | undefined,
* createdAt: string | undefined,
* firstName: string | null, // nullable properties are included
* lastName?: string, // optional properties are not included.
* }>;
*
* // is equal to
*
* type UndefinedProps = 'id' | 'createdAt' | 'firstName';
* ```
*/
export type NullishPropertiesOf<T> = {
[P in keyof T]-?: undefined extends T[P] ? P
: null extends T[P] ? P
: never
}[keyof T];
/**
* Makes all shallow properties of an object `optional` if they accept `undefined` or `null` as a value.
*
* @example
* ```typescript
* type MyOptionalType = MakeUndefinedOptional<{
* id: number | undefined,
* firstName: string,
* lastName: string | null,
* }>;
*
* // is equal to
*
* type MyOptionalType = {
* // this property is optional.
* id?: number | undefined,
* firstName: string,
* // this property is optional.
* lastName?: string | null,
* };
* ```
*/
export type MakeNullishOptional<T extends object> = Optional<T, NullishPropertiesOf<T>>;
/**
* Makes the type accept null & undefined
*/
export type Nullish<T> = T | null | undefined;
export type AllowArray<T> = T | T[];
export type AllowReadonlyArray<T> = T | readonly T[];