@nu-art/ts-common 0.201.18 → 0.201.19
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/utils/types.d.ts +43 -0
package/package.json
CHANGED
package/utils/types.d.ts
CHANGED
|
@@ -5,6 +5,49 @@ export type CustomOptional<T, K> = {
|
|
|
5
5
|
export type Subset<T> = {
|
|
6
6
|
[P in keyof T]: T[P];
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Utility type for creating a subset of keys (`AllKeys`) based on whether the corresponding value types in a mapping object (`Mapper`) extend a certain type (`ExpectedType`).
|
|
10
|
+
*
|
|
11
|
+
* @typeParam AllKeys - Represents all possible keys. Must be a string, a number, or a union of string and/or number literals.
|
|
12
|
+
* @typeParam Mapper - Represents a mapping object. Must be an object type with keys that are a subset of AllKeys and values of any type.
|
|
13
|
+
* @typeParam ExpectedType - Represents the type that the values in the Mapper object should extend.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* type Keys = 'a' | 'b' | 'c' | 'd';
|
|
17
|
+
* type MyMapper = { a: number, b: string, c: boolean, d: number };
|
|
18
|
+
* type MySubsetKeys = SubsetKeys<Keys, MyMapper, number>; // 'a' | 'd'
|
|
19
|
+
*/
|
|
20
|
+
export type SubsetKeys<AllKeys extends string | number | symbol, Mapper extends {
|
|
21
|
+
[K in AllKeys]: any;
|
|
22
|
+
}, ExpectedType> = {
|
|
23
|
+
[K in AllKeys]: Mapper[K] extends ExpectedType ? K : never;
|
|
24
|
+
}[AllKeys];
|
|
25
|
+
/**
|
|
26
|
+
* Utility type to generate a new type `T` where only the properties with keys listed in `Keys` are included.
|
|
27
|
+
* The resulting type will be a new object type with a subset of the properties from the original `T`.
|
|
28
|
+
*
|
|
29
|
+
* @typeParam T - The type from which the subset will be generated.
|
|
30
|
+
* @typeParam Keys - A set of keys from `T` which will be included in the new subset type.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* type MyType = { a: number, b: string, c: boolean, d: number };
|
|
34
|
+
* type MySubset = SubsetByKeys<MyType, 'a' | 'd'>; // { a: number, d: number }
|
|
35
|
+
*/
|
|
36
|
+
export type SubsetObjectByKeys<T, Keys extends keyof T> = {
|
|
37
|
+
[K in Keys]: T[K];
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Utility type to generate a subset of keys from a type `T` where the corresponding value type extends `ExpectedType`.
|
|
41
|
+
* It uses the `SubsetKeys` to get the subset of keys and `SubsetByKeys` to create a new type with only the properties that have those keys.
|
|
42
|
+
*
|
|
43
|
+
* @typeParam T - The type from which the subset of keys will be generated.
|
|
44
|
+
* @typeParam ExpectedType - The type that the values in `T` should extend.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* type MyType = { a: number, b: string, c: boolean, d: number };
|
|
48
|
+
* type MySubset = SubsetByValueType<MyType, number> // { a: number, d: number }
|
|
49
|
+
*/
|
|
50
|
+
export type SubsetObjectByValue<T, ExpectedType> = SubsetObjectByKeys<T, SubsetKeys<keyof T, T, ExpectedType>>;
|
|
8
51
|
export type OptionalKeys<T extends TS_Object> = Exclude<{
|
|
9
52
|
[K in keyof T]: T extends Record<K, T[K]> ? never : K;
|
|
10
53
|
}[keyof T], undefined>;
|