diff --git a/index.d.ts b/index.d.ts index cdce30faf..b8e0fe8db 100644 --- a/index.d.ts +++ b/index.d.ts @@ -42,6 +42,7 @@ export type {InvariantOf} from './source/invariant-of'; export type {SetOptional} from './source/set-optional'; export type {SetReadonly} from './source/set-readonly'; export type {SetRequired} from './source/set-required'; +export type {SetRequiredDeep} from './source/set-required-deep'; export type {SetNonNullable} from './source/set-non-nullable'; export type {ValueOf} from './source/value-of'; export type {AsyncReturnType} from './source/async-return-type'; diff --git a/readme.md b/readme.md index 48a9d7065..99177f5a9 100644 --- a/readme.md +++ b/readme.md @@ -157,6 +157,7 @@ Click the type names for complete docs. - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional. - [`SetReadonly`](source/set-readonly.d.ts) - Create a type that makes the given keys readonly. - [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required. +- [`SetRequiredDeep`](source/set-required-deep.d.ts) - Like `SetRequired` except it selects the keys deeply. - [`SetNonNullable`](source/set-non-nullable.d.ts) - Create a type that makes the given keys non-nullable. - [`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. - [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type. diff --git a/source/set-required-deep.d.ts b/source/set-required-deep.d.ts new file mode 100644 index 000000000..ec8a9a6c2 --- /dev/null +++ b/source/set-required-deep.d.ts @@ -0,0 +1,72 @@ +import type {NonRecursiveType} from './internal'; +import type {Paths} from './paths'; +import type {SimplifyDeep} from './simplify-deep'; + +/** +Create a type that makes the given keys required. You can specify deeply nested key paths. The remaining keys are kept as is. + +Use-case: You want to define as required only a some nested keys inside a model. + +@example +``` +import type {SetRequiredDeep} from 'type-fest'; + +type Foo = { + a?: number; + b?: string; + c?: { + d?: number + }[] +} + +type SomeRequiredDeep = SetRequiredDeep; +// type SomeRequiredDeep = { +// a: number; // Is now required +// b?: string; +// c: { +// d: number // Is now required +// }[] +// } +``` + +@category Object +*/ +export type SetRequiredDeep< + BaseType, + KeyPaths extends Paths, +> = BaseType extends NonRecursiveType + ? never + : SimplifyDeep< + BaseType extends Array + ? Array< + SetRequiredDeep< + NonNullable, + KeyPaths extends `${number}.${infer Rest extends Paths>}` + ? Rest + : never + > + > + : Omit> & + Required< + Pick< + BaseType, + Extract> + > + > & Partial<{ + [K in RootRequiredKeys< + BaseType, + Exclude> + >]: SetRequiredDeep< + NonNullable, + KeyPaths extends `${K}.${infer Rest extends Paths>}` + ? Rest + : never + >; + }> + >; + +// Extract the BaseType root keys from the KeyPaths +type RootRequiredKeys> = Extract< +keyof BaseType, +KeyPaths extends `${infer Root}.${string}` ? Root : KeyPaths +>; diff --git a/test-d/set-required-deep.ts b/test-d/set-required-deep.ts new file mode 100644 index 000000000..7a4ccd859 --- /dev/null +++ b/test-d/set-required-deep.ts @@ -0,0 +1,38 @@ +import {expectType} from 'tsd'; +import type {SetRequiredDeep} from '../index'; + +// Set nested key to required +declare const variation1: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b.c'>; +expectType<{a?: number; b?: {c: string}}>(variation1); + +// Set key to required but not nested keys if not specified +declare const variation2: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b'>; +expectType<{a?: number; b: {c?: string}}>(variation2); + +// Set root key to required +declare const variation3: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'a'>; +expectType<{a: number; b?: {c?: string}}>(variation3); + +// Keeps required key as required +declare const variation4: SetRequiredDeep<{a: number; b?: {c?: string}}, 'a'>; +expectType<{a: number; b?: {c?: string}}>(variation4); + +// Set key to required in a union. +declare const variation5: SetRequiredDeep<{a?: '1'; b?: {c?: boolean}} | {a?: '2'; b?: {c?: boolean}}, 'a'>; +expectType<{a: '1'; b?: {c?: boolean}} | {a: '2'; b?: {c?: boolean}}>(variation5); + +// Set array key to required +declare const variation6: SetRequiredDeep<{a?: Array<{b?: number}>}, 'a'>; +expectType<{a: Array<{b?: number}>}>(variation6); + +// Set key inside array to required +declare const variation7: SetRequiredDeep<{a?: Array<{b?: number}>}, `a.${number}.b`>; +expectType<{a?: Array<{b: number}>}>(variation7); + +// Set only specified keys inside array to required +declare const variation8: SetRequiredDeep<{a?: Array<{b?: number; c?: string}>}, `a.${number}.b`>; +expectType<{a?: Array<{b: number; c?: string}>}>(variation8); + +// Can set both root and nested keys to required +declare const variation9: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b' | 'b.c'>; +expectType<{a?: number; b: {c: string}}>(variation9);