From 62bfe33e5bcc882b97b7f9e2335d57ee2e6c33e3 Mon Sep 17 00:00:00 2001 From: didi <1191340528@qq.com> Date: Fri, 7 Aug 2020 16:34:57 +0800 Subject: [PATCH 1/2] fix: FunctionKeys and NonFunctionKeys does not filter undefined and null --- src/mapped-types.spec.ts | 2 ++ src/mapped-types.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/mapped-types.spec.ts b/src/mapped-types.spec.ts index a6de52c..1b844b1 100644 --- a/src/mapped-types.spec.ts +++ b/src/mapped-types.spec.ts @@ -55,6 +55,8 @@ type MixedProps = { setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any; + undef: undefined; + unNull: null; }; type ReadWriteProps = { readonly a: number; b: string }; type RequiredOptionalProps = { diff --git a/src/mapped-types.ts b/src/mapped-types.ts index 54395d9..1fd9ecf 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -75,7 +75,9 @@ export type NonUndefined = A extends undefined ? never : A; * type Keys = FunctionKeys; */ export type FunctionKeys = { - [K in keyof T]-?: NonUndefined extends Function ? K : never; + [P in keyof T]-?: SetIntersection, Function> extends never + ? never + : P; }[keyof T]; /** @@ -88,7 +90,9 @@ export type FunctionKeys = { * type Keys = NonFunctionKeys; */ export type NonFunctionKeys = { - [K in keyof T]-?: NonUndefined extends Function ? never : K; + [P in keyof T]-?: SetDifference, Function> extends never + ? never + : P; }[keyof T]; /** From ad439acb1561818366aedec1683ba8842d1a014e Mon Sep 17 00:00:00 2001 From: didi <1191340528@qq.com> Date: Fri, 7 Aug 2020 16:59:03 +0800 Subject: [PATCH 2/2] chore(mapped-types): fix NonFunctionKeys should be not need to filter null type --- src/__snapshots__/mapped-types.spec.ts.snap | 2 +- src/mapped-types.spec.snap.ts | 4 +++- src/mapped-types.ts | 4 +--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/__snapshots__/mapped-types.spec.ts.snap b/src/__snapshots__/mapped-types.spec.ts.snap index b8e2e49..578829c 100644 --- a/src/__snapshots__/mapped-types.spec.ts.snap +++ b/src/__snapshots__/mapped-types.spec.ts.snap @@ -125,7 +125,7 @@ exports[`Mutable testType>['visible']>(true) (type) shou exports[`MutableKeys testType>() (type) should match snapshot 1`] = `"\\"b\\""`; -exports[`NonFunctionKeys testType>() (type) should match snapshot 1`] = `"\\"name\\" | \\"someKeys\\""`; +exports[`NonFunctionKeys testType>() (type) should match snapshot 1`] = `"\\"name\\" | \\"someKeys\\" | \\"unNull\\""`; exports[`NonUndefined testType>() (type) should match snapshot 1`] = `"string | null"`; diff --git a/src/mapped-types.spec.snap.ts b/src/mapped-types.spec.snap.ts index a4397ac..4735734 100644 --- a/src/mapped-types.spec.snap.ts +++ b/src/mapped-types.spec.snap.ts @@ -55,6 +55,8 @@ type MixedProps = { setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any; + undef: undefined; + unNull: null; }; type ReadWriteProps = { readonly a: number; b: string }; type RequiredOptionalProps = { @@ -112,7 +114,7 @@ type RequiredOptionalProps = { // @dts-jest:group NonFunctionKeys { - // @dts-jest:pass:snap -> "name" | "someKeys" + // @dts-jest:pass:snap -> "name" | "someKeys" | "unNull" testType>(); } diff --git a/src/mapped-types.ts b/src/mapped-types.ts index 1fd9ecf..bec6ff8 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -90,9 +90,7 @@ export type FunctionKeys = { * type Keys = NonFunctionKeys; */ export type NonFunctionKeys = { - [P in keyof T]-?: SetDifference, Function> extends never - ? never - : P; + [K in keyof T]-?: NonUndefined extends Function ? never : K; }[keyof T]; /**