@@ -19,3 +19,29 @@ export const mapBy = <K extends keyof T, T>(items: T[] | undefined, key: K): Map
19
19
20
20
export const concatMap = < K , V > ( map1 : Map < K , V > , map2 : Map < K , V > ) =>
21
21
new Map ( [ ...map1 . entries ( ) , ...map2 . entries ( ) ] ) ;
22
+
23
+ /**
24
+ * Transform the provided keys of an object to be non-nullable.
25
+ *
26
+ * -? removes the optional modifier
27
+ *
28
+ * NonNullable removes the nullable modifier
29
+ * @param Type The object type we want to transform
30
+ * @param K The keys we want to make non-nullable
31
+ * @example NonNullableObject<{ a?: string | null; b?: string; c?: string }, 'a' | 'c'> = { a: string; b?: string; c: string }
32
+ */
33
+ type NonNullableObject < Type , K extends keyof Type > = {
34
+ [ Property in keyof Type as Property extends K ? Property : never ] -?: NonNullable < Type [ Property ] > ;
35
+ } & {
36
+ [ Property in keyof Type as Property extends K ? never : Property ] : Type [ Property ] ;
37
+ } ;
38
+
39
+ /**
40
+ * Shortcut type to pick some properties of an object and make some of them non-nullable.
41
+ * @example PickAndNonNullableFields<{ a?: string | null; b?: string; c?: string }, 'a' | 'c', 'a'> = { a: string; c?: string }
42
+ */
43
+ export type PickAndNonNullableFields <
44
+ Type ,
45
+ PickKeys extends keyof Type ,
46
+ TransformKeys extends keyof Type ,
47
+ > = NonNullableObject < Pick < Type , PickKeys > , Extract < TransformKeys , PickKeys > > ;
0 commit comments