@lipemat/js-boilerplate-shared 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/types/utility.d.ts +88 -0
package/package.json
CHANGED
package/types/utility.d.ts
CHANGED
|
@@ -4,3 +4,91 @@
|
|
|
4
4
|
* @example AtLeast<Post, 'author' | 'post_title'> - post_title and author will be required, the rest optional.
|
|
5
5
|
*/
|
|
6
6
|
export type AtLeast<T, K extends keyof T> = Partial<T> & Required<Pick<T, K>>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Same as `keyof` except only keys whose value of a specified
|
|
10
|
+
* type will be returned.
|
|
11
|
+
*
|
|
12
|
+
* @example ```ts
|
|
13
|
+
* type Post = {id: number, title: string, url: string}
|
|
14
|
+
* KeysOfType<Post, string> - 'id' will not be included because is not string = 'title'|'url'
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export type KeysOfType<T, K> = { [I in keyof T]: T[I] extends K ? I : never }[keyof T]
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Removes all the properties of type never, even the deeply nested ones.
|
|
21
|
+
*
|
|
22
|
+
* Used to provider different types based on context by assigning
|
|
23
|
+
* some properties to never.
|
|
24
|
+
*
|
|
25
|
+
* @notice Will not work properly of sub items are an `interface` they
|
|
26
|
+
* must be a `type`.
|
|
27
|
+
*
|
|
28
|
+
* @example OmitNever<{foo: number; bar: never}> - {foo: number}
|
|
29
|
+
*/
|
|
30
|
+
export type OmitNever<T, Converted = {
|
|
31
|
+
[K in keyof T]:
|
|
32
|
+
// If the type of this property (excluding | undefined) is never set it to never.
|
|
33
|
+
Exclude<T[ K ], undefined> extends never ? never :
|
|
34
|
+
// If the type of this property is an object, send it through again.
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-restricted-types
|
|
36
|
+
T[ K ] extends Record<string, unknown> ? OmitNever<T[ K ]> : T[ K ];
|
|
37
|
+
}> = Pick<Converted, {
|
|
38
|
+
// List all properties which were not set to never above.
|
|
39
|
+
[K in keyof Converted]: Converted[ K ] extends never ? never : K;
|
|
40
|
+
}[ keyof Converted ]>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Make specified properties on a type optional.
|
|
44
|
+
*
|
|
45
|
+
* @example `Optional<Post, 'post_title'|'author'>` - `post_title` and `author` will be optional.
|
|
46
|
+
*/
|
|
47
|
+
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Make specified properties on a type required.
|
|
51
|
+
* Leave the rest of the properties as is.
|
|
52
|
+
*
|
|
53
|
+
* @example Require<UserUpdate, 'id'> - `id` will be required, the rest left as is.
|
|
54
|
+
*/
|
|
55
|
+
export type Require<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Subtract one types properties from another.
|
|
59
|
+
*
|
|
60
|
+
* @example Subtract<Post, {id: number, post_title: string}> - id and post_title
|
|
61
|
+
* will be removed from the Post definition.
|
|
62
|
+
*/
|
|
63
|
+
export type Subtract<T extends K, K> = Omit<T, keyof K>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Exclude all top-level properties in an object that are of a type.
|
|
67
|
+
*
|
|
68
|
+
* @example ExcludeOfType<{y:'false':x:false}, boolean> = {y:'false'}
|
|
69
|
+
*/
|
|
70
|
+
export type ExcludeOfType<T, Type> = Pick<T, {
|
|
71
|
+
[P in keyof T]: T[P] extends Type ? never : P
|
|
72
|
+
}[keyof T]>
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Union from array elements.
|
|
76
|
+
*
|
|
77
|
+
* Useful for passing to `Pick`.
|
|
78
|
+
*
|
|
79
|
+
* @example UnionOfArray<['one', 2, 'three']> - `'one' | 2 | 'three'`.
|
|
80
|
+
*/
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/no-restricted-types
|
|
82
|
+
export type UnionOfArray<A extends Readonly<unknown[]>> = A[number];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Combine two types, making all properties optional which
|
|
86
|
+
* do not intersect.
|
|
87
|
+
*
|
|
88
|
+
* @example OptionalNonIntersect<{foo: string, bar: number}, {foo: string, waz: boolean}> - `{foo: string, bar?: number, waz?: boolean}`.
|
|
89
|
+
*/
|
|
90
|
+
export type OptionalNonIntersect<T, U> =
|
|
91
|
+
Pick<T, Extract<keyof T, keyof U>> &
|
|
92
|
+
{
|
|
93
|
+
[P in Exclude<keyof T | keyof U, Extract<keyof T, keyof U>>]?: P extends keyof T ? T[P] : P extends keyof U ? U[P] : never;
|
|
94
|
+
};
|