From f36066b7c08dc7ac1fcdb57b39c242eee4dfa35e Mon Sep 17 00:00:00 2001 From: shanamaid Date: Tue, 7 May 2019 20:39:08 +0800 Subject: [PATCH] test: add test unit --- src/__snapshots__/mapped-types.spec.ts.snap | 4 ++++ src/mapped-types.spec.ts | 10 ++++++++++ src/mapped-types.ts | 12 ++++++------ 3 files changed, 20 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.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;