From 0f30e1e3aecb814ccfe02f7829849a77abb7df05 Mon Sep 17 00:00:00 2001 From: shanamaid Date: Tue, 7 May 2019 15:43:24 +0800 Subject: [PATCH 1/7] feat: add PartOptional --- src/mapped-types.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/mapped-types.ts b/src/mapped-types.ts index 16138de..c590273 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -554,3 +554,19 @@ type IfEquals = (() => T extends X * gross(eur); */ export type Brand = T & { __brand: U }; + +/** + * PartOptional + * @desc From `T` make a set of properties by key `K` become optional + * @example + * type Props = { + * name: string; + * age: string; + * height: number; + * }; + * // Expect: { name: string; age?: string; height?: number; } + * type Props = PartOptional; + */ +export type PartOptional = Omit & { + [key in K]?: T[key]; +}; From c20cc552f4b994843a8ff55c45757566326bbbd4 Mon Sep 17 00:00:00 2001 From: shanamaid Date: Tue, 7 May 2019 15:49:10 +0800 Subject: [PATCH 2/7] feat: add entry --- src/index.ts | 1 + src/mapped-types.ts | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 57e2883..f1bb1d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,7 @@ export { OmitByValueExact, OptionalKeys, Overwrite, + PartOptional, PickByValue, PickByValueExact, Primitive, diff --git a/src/mapped-types.ts b/src/mapped-types.ts index c590273..f339059 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -567,6 +567,5 @@ export type Brand = T & { __brand: U }; * // Expect: { name: string; age?: string; height?: number; } * type Props = PartOptional; */ -export type PartOptional = Omit & { - [key in K]?: T[key]; -}; +export type PartOptional = Omit & + { [key in K]?: T[key] }; From e452ddb69c51ff4de09a54e357089d743bee2329 Mon Sep 17 00:00:00 2001 From: shanamaid Date: Tue, 7 May 2019 20:20:04 +0800 Subject: [PATCH 3/7] refactor: rename optional --- src/index.ts | 2 +- src/mapped-types.ts | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index f1bb1d5..2022ff1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,7 +35,7 @@ export { OmitByValueExact, OptionalKeys, Overwrite, - PartOptional, + Optional, PickByValue, PickByValueExact, Primitive, diff --git a/src/mapped-types.ts b/src/mapped-types.ts index f339059..79c69dc 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -556,7 +556,7 @@ type IfEquals = (() => T extends X export type Brand = T & { __brand: U }; /** - * PartOptional + * Optional * @desc From `T` make a set of properties by key `K` become optional * @example * type Props = { @@ -564,8 +564,12 @@ export type Brand = T & { __brand: U }; * age: string; * height: number; * }; + * + * // Expect: { name?: string; age?: string; height?: number; } + * type Props = Optional; + * * // Expect: { name: string; age?: string; height?: number; } - * type Props = PartOptional; + * type Props = Optional; */ -export type PartOptional = Omit & - { [key in K]?: T[key] }; +export type Optional = K extends (keyof T) ? (Omit & + { [key in K]?: T[key] }) : Partial; From 9c9247984910175229a92989a4cac91454bea1bd Mon Sep 17 00:00:00 2001 From: shanamaid Date: Tue, 7 May 2019 20:20:49 +0800 Subject: [PATCH 4/7] chore: formatter --- src/mapped-types.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mapped-types.ts b/src/mapped-types.ts index 79c69dc..7c36213 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -571,5 +571,6 @@ export type Brand = T & { __brand: U }; * // Expect: { name: string; age?: string; height?: number; } * type Props = Optional; */ -export type Optional = K extends (keyof T) ? (Omit & - { [key in K]?: T[key] }) : Partial; +export type Optional = K extends (keyof T) + ? (Omit & { [key in K]?: T[key] }) + : Partial; From 42f36a09ec94e7c31d1e324e3e8453cfa985b727 Mon Sep 17 00:00:00 2001 From: shanamaid Date: Tue, 7 May 2019 20:32:15 +0800 Subject: [PATCH 5/7] docs: update md --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index bfbe78a..6766611 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ Issues can be funded by anyone and the money will be transparently distributed t * [`ReadonlyKeys`](#readonlykeyst) * [`WritableKeys`](#writablekeyst) * [`RequiredKeys`](#requiredkeyst) +* [`Optional`](#optionalst) * [`OptionalKeys`](#optionalkeyst) * [`Partial`](#partialt) _(built-in)_ * [`DeepPartial`](#deeppartialt) @@ -313,6 +314,25 @@ type RequiredProps = ReadonlyKeys; [⇧ back to top](#table-of-contents) +### `Optional` + +From `T` make a set of properties by key `K` become optional + +**Usage:** + +```ts +import { Optional } from 'utility-types'; + +type Props = { name: string; age: string; height: number; }; + +// Expect: { name?: string; age?: string; height?: number; } +type Props = Optional +// Expect: { name: string; age?: string; height?: number; } +type Props = Optional; +``` + +[⇧ back to top](#table-of-contents) + ### `OptionalKeys` Get union type of keys that are optional in object type `T` From d69c2ddf27240327470abea5067885e387c4fb1a Mon Sep 17 00:00:00 2001 From: shanamaid Date: Tue, 7 May 2019 20:40:33 +0800 Subject: [PATCH 6/7] test: add test unit --- src/__snapshots__/mapped-types.spec.ts.snap | 4 ++++ src/mapped-types.spec.snap.ts | 10 ++++++++++ src/mapped-types.spec.ts | 10 ++++++++++ src/mapped-types.ts | 12 ++++++------ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/__snapshots__/mapped-types.spec.ts.snap b/src/__snapshots__/mapped-types.spec.ts.snap index 8d2bc3b..2380a68 100644 --- a/src/__snapshots__/mapped-types.spec.ts.snap +++ b/src/__snapshots__/mapped-types.spec.ts.snap @@ -126,6 +126,10 @@ exports[`OmitByValueExact testType>() 1`] = `"Pick"`; +exports[`Optional testType>() 1`] = `"(Pick & { age?: number | undefined; }) | (Pick & { visible?: boolean | undefined; })"`; + +exports[`Optional testType>() 1`] = `"Partial"`; + exports[`OptionalKeys testType>() 1`] = `"\\"opt\\" | \\"optUndef\\""`; exports[`Overwrite const result: Overwrite, T> = rest 1`] = `"any"`; diff --git a/src/mapped-types.spec.snap.ts b/src/mapped-types.spec.snap.ts index eea4041..caf747f 100644 --- a/src/mapped-types.spec.snap.ts +++ b/src/mapped-types.spec.snap.ts @@ -38,6 +38,7 @@ import { OptionalKeys, PickByValueExact, OmitByValueExact, + Optional, } from './mapped-types'; /** @@ -482,3 +483,12 @@ type RequiredOptionalProps = { // @dts-jest:pass:snap -> Brand testType>(); } + +// @dts-jest:group Optional +{ + // @dts-jest:pass:snap -> Partial + testType>(); + + // @dts-jest:pass:snap -> (Pick & { age?: number | undefined; }) | (Pick & { visible?: boolean | undefined; }) + testType>(); +} diff --git a/src/mapped-types.spec.ts b/src/mapped-types.spec.ts index 0f37d73..0859e27 100644 --- a/src/mapped-types.spec.ts +++ b/src/mapped-types.spec.ts @@ -38,6 +38,7 @@ import { OptionalKeys, PickByValueExact, OmitByValueExact, + Optional, } from './mapped-types'; /** @@ -482,3 +483,12 @@ type RequiredOptionalProps = { // @dts-jest:pass:snap testType>(); } + +// @dts-jest:group Optional +{ + // @dts-jest:pass:snap + testType>(); + + // @dts-jest:pass:snap + testType>(); +} diff --git a/src/mapped-types.ts b/src/mapped-types.ts index 7c36213..d9a288a 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -561,16 +561,16 @@ export type Brand = T & { __brand: U }; * @example * type Props = { * name: string; - * age: string; - * height: number; + * age: number; + * visible: boolean; * }; * - * // Expect: { name?: string; age?: string; height?: number; } + * // Expect: { name?: string; age?: number; visible?: boolean; } * type Props = Optional; * - * // Expect: { name: string; age?: string; height?: number; } - * type Props = Optional; + * // Expect: { name: string; age?: number; visible?: boolean; } + * type Props = Optional; */ -export type Optional = K extends (keyof T) +export type Optional = K extends (keyof T) ? (Omit & { [key in K]?: T[key] }) : Partial; From 8e0f4f1aed9bfa3396b2bd17187915b924e88b3b Mon Sep 17 00:00:00 2001 From: shanamaid Date: Tue, 7 May 2019 20:43:06 +0800 Subject: [PATCH 7/7] docs: update md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6766611..6fed5ba 100644 --- a/README.md +++ b/README.md @@ -323,12 +323,12 @@ From `T` make a set of properties by key `K` become optional ```ts import { Optional } from 'utility-types'; -type Props = { name: string; age: string; height: number; }; +type Props = { name: string; age: number; visilbe: boolean; }; -// Expect: { name?: string; age?: string; height?: number; } +// Expect: { name?: string; age?: number; visilbe?: boolean; } type Props = Optional -// Expect: { name: string; age?: string; height?: number; } -type Props = Optional; +// Expect: { name: string; age?: number; visilbe?: boolean; } +type Props = Optional; ``` [⇧ back to top](#table-of-contents)