Skip to content

Commit 3d54627

Browse files
hugomartinetsindresorhussom-sm
authored
Add SetRequiredDeep type (#939)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com> Co-authored-by: Som Shekhar Mukherjee <iamssmkhrj@gmail.com>
1 parent eff76da commit 3d54627

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type {InvariantOf} from './source/invariant-of';
4242
export type {SetOptional} from './source/set-optional';
4343
export type {SetReadonly} from './source/set-readonly';
4444
export type {SetRequired} from './source/set-required';
45+
export type {SetRequiredDeep} from './source/set-required-deep';
4546
export type {SetNonNullable} from './source/set-non-nullable';
4647
export type {ValueOf} from './source/value-of';
4748
export type {AsyncReturnType} from './source/async-return-type';

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Click the type names for complete docs.
159159
- [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
160160
- [`SetReadonly`](source/set-readonly.d.ts) - Create a type that makes the given keys readonly.
161161
- [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
162+
- [`SetRequiredDeep`](source/set-required-deep.d.ts) - Like `SetRequired` except it selects the keys deeply.
162163
- [`SetNonNullable`](source/set-non-nullable.d.ts) - Create a type that makes the given keys non-nullable.
163164
- [`ValueOf`](source/value-of.d.ts) - Create a union of the given object's values, and optionally specify which keys to get the values from.
164165
- [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type.

source/set-required-deep.d.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type {NonRecursiveType, StringToNumber} from './internal';
2+
import type {Paths} from './paths';
3+
import type {SimplifyDeep} from './simplify-deep';
4+
import type {UnknownArray} from './unknown-array';
5+
6+
/**
7+
Create a type that makes the given keys required. You can specify deeply nested key paths. The remaining keys are kept as is.
8+
9+
Use-case: Selectively make nested properties required in complex types like models.
10+
11+
@example
12+
```
13+
import type {SetRequiredDeep} from 'type-fest';
14+
15+
type Foo = {
16+
a?: number;
17+
b?: string;
18+
c?: {
19+
d?: number
20+
}[]
21+
}
22+
23+
type SomeRequiredDeep = SetRequiredDeep<Foo, 'a' | `c.${number}.d`>;
24+
// type SomeRequiredDeep = {
25+
// a: number; // Is now required
26+
// b?: string;
27+
// c: {
28+
// d: number // Is now required
29+
// }[]
30+
// }
31+
```
32+
33+
@category Object
34+
*/
35+
export type SetRequiredDeep<BaseType, KeyPaths extends Paths<BaseType>> =
36+
BaseType extends NonRecursiveType
37+
? BaseType
38+
: SimplifyDeep<(
39+
BaseType extends UnknownArray
40+
? {}
41+
: {[K in keyof BaseType as K extends (KeyPaths | StringToNumber<KeyPaths & string>) ? K : never]-?: BaseType[K]}
42+
) & {
43+
[K in keyof BaseType]: Extract<KeyPaths, `${K & (string | number)}.${string}`> extends never
44+
? BaseType[K]
45+
: SetRequiredDeep<BaseType[K], KeyPaths extends `${K & (string | number)}.${infer Rest extends Paths<BaseType[K]>}` ? Rest : never>
46+
}>;

test-d/set-required-deep.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {expectType} from 'tsd';
2+
import type {SetRequiredDeep} from '../index';
3+
4+
// Set nested key to required
5+
declare const variation1: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b.c'>;
6+
expectType<{a?: number; b?: {c: string}}>(variation1);
7+
8+
// Set key to required but not nested keys if not specified
9+
declare const variation2: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b'>;
10+
expectType<{a?: number; b: {c?: string}}>(variation2);
11+
12+
// Set root key to required
13+
declare const variation3: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'a'>;
14+
expectType<{a: number; b?: {c?: string}}>(variation3);
15+
16+
// Keeps required key as required
17+
declare const variation4: SetRequiredDeep<{a: number; b?: {c?: string}}, 'a'>;
18+
expectType<{a: number; b?: {c?: string}}>(variation4);
19+
20+
// Set key to required in a union.
21+
declare const variation5: SetRequiredDeep<{a?: '1'; b?: {c?: boolean}} | {a?: '2'; b?: {c?: boolean}}, 'a'>;
22+
expectType<{a: '1'; b?: {c?: boolean}} | {a: '2'; b?: {c?: boolean}}>(variation5);
23+
24+
// Set array key to required
25+
declare const variation6: SetRequiredDeep<{a?: Array<{b?: number}>}, 'a'>;
26+
expectType<{a: Array<{b?: number}>}>(variation6);
27+
28+
// Set key inside array to required
29+
declare const variation7: SetRequiredDeep<{a?: Array<{b?: number}>}, `a.${number}.b`>;
30+
expectType<{a?: Array<{b: number}>}>(variation7);
31+
32+
// Set only specified keys inside array to required
33+
declare const variation8: SetRequiredDeep<{a?: Array<{b?: number; c?: string}>}, `a.${number}.b`>;
34+
expectType<{a?: Array<{b: number; c?: string}>}>(variation8);
35+
36+
// Can set both root and nested keys to required
37+
declare const variation9: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b' | 'b.c'>;
38+
expectType<{a?: number; b: {c: string}}>(variation9);
39+
40+
// Preserves required root keys
41+
declare const variation10: SetRequiredDeep<{a: 1; b: {c?: 1}}, 'b.c'>;
42+
expectType<{a: 1; b: {c: 1}}>(variation10);
43+
44+
// Preserves union in root keys
45+
declare const variation11: SetRequiredDeep<{a: 1; b: {c?: 1} | number}, 'b.c'>;
46+
expectType<{a: 1; b: {c: 1} | number}>(variation11);
47+
48+
// Preserves readonly in root keys
49+
declare const variation12: SetRequiredDeep<{a: 1; readonly b: {c?: 1}}, 'b.c'>;
50+
expectType<{a: 1; readonly b: {c: 1}}>(variation12);
51+
52+
// Works with number keys
53+
declare const variation13: SetRequiredDeep<{0: 1; 1: {2?: string}}, '1.2'>;
54+
expectType<{0: 1; 1: {2: string}}>(variation13);
55+
56+
// Multiple keys
57+
declare const variation14: SetRequiredDeep<{a?: 1; b?: {c?: 2}; d?: {e?: {f?: 2}; g?: 3}}, 'a' | 'b' | 'b.c' | 'd.e.f' | 'd.g'>;
58+
expectType<{a: 1; b: {c: 2}; d?: {e?: {f: 2}; g: 3}}>(variation14);
59+
60+
// Index signatures
61+
declare const variation15: SetRequiredDeep<{[x: string]: any; a?: number; b?: {c?: number}}, 'a' | 'b.c'>;
62+
expectType<{[x: string]: any; a: number; b?: {c: number}}>(variation15);

0 commit comments

Comments
 (0)