From 7d4346a304e6a3726686543b698cef63777cfeb3 Mon Sep 17 00:00:00 2001 From: Igor Kalinovskiy Date: Sat, 9 Nov 2019 18:10:26 +1000 Subject: [PATCH 1/2] Add snapshot-test for `keyof` (Pick|Omit)ByValue(Exact)? --- src/__snapshots__/mapped-types.spec.ts.snap | 8 ++++++++ src/mapped-types.spec.snap.ts | 8 ++++++++ src/mapped-types.spec.ts | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/src/__snapshots__/mapped-types.spec.ts.snap b/src/__snapshots__/mapped-types.spec.ts.snap index 56598e5..0a4268e 100644 --- a/src/__snapshots__/mapped-types.spec.ts.snap +++ b/src/__snapshots__/mapped-types.spec.ts.snap @@ -147,6 +147,8 @@ exports[`OmitByValue testType>() ( exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\" | undefined"`; + exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; @@ -155,6 +157,8 @@ exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\" | undefined"`; + exports[`Optional testType>({ name: 'Yolo' }) (type) should match snapshot 1`] = `"Optional"`; exports[`Optional testType>({ name: 'Yolo', age: 99 }) (type) should match snapshot 1`] = `"Optional"`; @@ -177,6 +181,8 @@ exports[`PickByValue testType>() ( exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValue testType>() (type) should match snapshot 1`] = `"\\"req\\" | undefined"`; + exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; @@ -185,6 +191,8 @@ exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"\\"req\\" | undefined"`; + exports[`PromiseType testType>>() (type) should match snapshot 1`] = `"string"`; exports[`ReadonlyKeys testType>() (type) should match snapshot 1`] = `"\\"a\\""`; diff --git a/src/mapped-types.spec.snap.ts b/src/mapped-types.spec.snap.ts index ab1f567..6e0aa20 100644 --- a/src/mapped-types.spec.snap.ts +++ b/src/mapped-types.spec.snap.ts @@ -148,6 +148,8 @@ type RequiredOptionalProps = { testType>(); // @dts-jest:pass:snap -> Pick testType>(); + // @dts-jest:pass:snap -> "req" | undefined + testType>(); const fn = (props: T) => { // @dts-jest:pass:snap -> Pick @@ -163,6 +165,8 @@ type RequiredOptionalProps = { testType>(); // @dts-jest:pass:snap -> Pick testType>(); + // @dts-jest:pass:snap -> "req" | undefined + testType>(); const fn = (props: T) => { // @dts-jest:pass:snap -> Pick @@ -195,6 +199,8 @@ type RequiredOptionalProps = { testType>(); // @dts-jest:pass:snap -> Pick testType>(); + // @dts-jest:pass:snap -> "reqUndef" | "opt" | "optUndef" | undefined + testType>(); const fn = (props: T) => { // @dts-jest:pass:snap -> Pick @@ -210,6 +216,8 @@ type RequiredOptionalProps = { testType>(); // @dts-jest:pass:snap -> Pick testType>(); + // @dts-jest:pass:snap -> "reqUndef" | "opt" | "optUndef" | undefined + testType>(); const fn = (props: T) => { // @dts-jest:pass:snap -> Pick diff --git a/src/mapped-types.spec.ts b/src/mapped-types.spec.ts index f828cde..a6de52c 100644 --- a/src/mapped-types.spec.ts +++ b/src/mapped-types.spec.ts @@ -148,6 +148,8 @@ type RequiredOptionalProps = { testType>(); // @dts-jest:pass:snap testType>(); + // @dts-jest:pass:snap + testType>(); const fn = (props: T) => { // @dts-jest:pass:snap @@ -163,6 +165,8 @@ type RequiredOptionalProps = { testType>(); // @dts-jest:pass:snap testType>(); + // @dts-jest:pass:snap + testType>(); const fn = (props: T) => { // @dts-jest:pass:snap @@ -195,6 +199,8 @@ type RequiredOptionalProps = { testType>(); // @dts-jest:pass:snap testType>(); + // @dts-jest:pass:snap + testType>(); const fn = (props: T) => { // @dts-jest:pass:snap @@ -210,6 +216,8 @@ type RequiredOptionalProps = { testType>(); // @dts-jest:pass:snap testType>(); + // @dts-jest:pass:snap + testType>(); const fn = (props: T) => { // @dts-jest:pass:snap From a73fbf014027324f92620d88271bcc21cb6b62fd Mon Sep 17 00:00:00 2001 From: Igor Kalinovskiy Date: Sat, 9 Nov 2019 18:18:44 +1000 Subject: [PATCH 2/2] Fix (Pick|Omit)ByValue(Exact)? to not contain `undefined` in `keyof` of their result --- src/__snapshots__/mapped-types.spec.ts.snap | 40 ++++++++++----------- src/mapped-types.spec.snap.ts | 40 ++++++++++----------- src/mapped-types.ts | 8 ++--- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/__snapshots__/mapped-types.spec.ts.snap b/src/__snapshots__/mapped-types.spec.ts.snap index 0a4268e..b8e2e49 100644 --- a/src/__snapshots__/mapped-types.spec.ts.snap +++ b/src/__snapshots__/mapped-types.spec.ts.snap @@ -139,25 +139,25 @@ exports[`Omit testType>() (type) should match snapshot 1`] = exports[`Omit testType>() (type) should match snapshot 1`] = `"Pick>"`; -exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\" | undefined"`; +exports[`OmitByValue testType>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\""`; -exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\" | undefined"`; +exports[`OmitByValueExact testType>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\""`; exports[`Optional testType>({ name: 'Yolo' }) (type) should match snapshot 1`] = `"Optional"`; @@ -173,25 +173,25 @@ exports[`Overwrite const result: Overwrite, T> = rest (type) shou exports[`Overwrite testType>() (type) should match snapshot 1`] = `"Pick & Pick, \\"name\\" | \\"age\\" | \\"visible\\">"`; -exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValue testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`PickByValue testType>() (type) should match snapshot 1`] = `"\\"req\\" | undefined"`; +exports[`PickByValue testType>() (type) should match snapshot 1`] = `"\\"req\\""`; -exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; +exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"Pick"`; -exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"\\"req\\" | undefined"`; +exports[`PickByValueExact testType>() (type) should match snapshot 1`] = `"\\"req\\""`; exports[`PromiseType testType>>() (type) should match snapshot 1`] = `"string"`; diff --git a/src/mapped-types.spec.snap.ts b/src/mapped-types.spec.snap.ts index 6e0aa20..a4397ac 100644 --- a/src/mapped-types.spec.snap.ts +++ b/src/mapped-types.spec.snap.ts @@ -142,34 +142,34 @@ type RequiredOptionalProps = { // @dts-jest:group PickByValue { - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> "req" | undefined + // @dts-jest:pass:snap -> "req" testType>(); const fn = (props: T) => { - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); }; } // @dts-jest:group PickByValueExact { - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> "req" | undefined + // @dts-jest:pass:snap -> "req" testType>(); const fn = (props: T) => { - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); }; } @@ -193,34 +193,34 @@ type RequiredOptionalProps = { // @dts-jest:group OmitByValue { - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> "reqUndef" | "opt" | "optUndef" | undefined + // @dts-jest:pass:snap -> "reqUndef" | "opt" | "optUndef" testType>(); const fn = (props: T) => { - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); }; } // @dts-jest:group OmitByValueExact { - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); - // @dts-jest:pass:snap -> "reqUndef" | "opt" | "optUndef" | undefined + // @dts-jest:pass:snap -> "reqUndef" | "opt" | "optUndef" testType>(); const fn = (props: T) => { - // @dts-jest:pass:snap -> Pick + // @dts-jest:pass:snap -> Pick testType>(); }; } diff --git a/src/mapped-types.ts b/src/mapped-types.ts index 2acbe0d..54395d9 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -190,7 +190,7 @@ namespace Pick {} */ export type PickByValue = Pick< T, - { [Key in keyof T]: T[Key] extends ValueType ? Key : never }[keyof T] + { [Key in keyof T]-?: T[Key] extends ValueType ? Key : never }[keyof T] >; /** @@ -207,7 +207,7 @@ export type PickByValue = Pick< export type PickByValueExact = Pick< T, { - [Key in keyof T]: [ValueType] extends [T[Key]] + [Key in keyof T]-?: [ValueType] extends [T[Key]] ? [T[Key]] extends [ValueType] ? Key : never @@ -240,7 +240,7 @@ export type Omit = Pick>; */ export type OmitByValue = Pick< T, - { [Key in keyof T]: T[Key] extends ValueType ? never : Key }[keyof T] + { [Key in keyof T]-?: T[Key] extends ValueType ? never : Key }[keyof T] >; /** @@ -257,7 +257,7 @@ export type OmitByValue = Pick< export type OmitByValueExact = Pick< T, { - [Key in keyof T]: [ValueType] extends [T[Key]] + [Key in keyof T]-?: [ValueType] extends [T[Key]] ? [T[Key]] extends [ValueType] ? never : Key