From 4f14bff7321b9a9876dca0719945423abd6b4da1 Mon Sep 17 00:00:00 2001 From: Haozheng Li Date: Sun, 17 Mar 2024 02:01:06 +0800 Subject: [PATCH] `OmitDeep`: Fix handling if path not matched (#834) --- source/omit-deep.d.ts | 14 +++++++++----- test-d/omit-deep.ts | 14 +++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/source/omit-deep.d.ts b/source/omit-deep.d.ts index 402a4309e..d1831e5aa 100644 --- a/source/omit-deep.d.ts +++ b/source/omit-deep.d.ts @@ -1,6 +1,8 @@ import type {ArraySplice} from './array-splice'; import type {ExactKey, IsArrayReadonly, NonRecursiveType, SetArrayAccess, ToString} from './internal'; import type {IsEqual} from './is-equal'; +import type {IsNever} from './is-never'; +import type {LiteralUnion} from './literal-union'; import type {SimplifyDeep} from './merge-deep'; import type {Paths} from './paths'; import type {SharedUnionFieldsDeep} from './shared-union-fields-deep'; @@ -69,7 +71,7 @@ type AddressInfo = OmitDeep; @category Object @category Array */ -export type OmitDeep> = +export type OmitDeep, string>> = SimplifyDeep< SharedUnionFieldsDeep< {[P in PathUnion]: OmitDeepWithOnePath}[PathUnion] @@ -102,9 +104,11 @@ P extends `${infer RecordKeyInPath}.${infer SubPath}` : ObjectT[Key] } : ExactKey extends infer Key - ? Key extends PropertyKey - ? Omit - : ObjectT + ? IsNever extends true + ? ObjectT + : Key extends PropertyKey + ? Omit + : ObjectT : ObjectT; /** @@ -133,4 +137,4 @@ type OmitDeepArrayWithOnePath - : never; + : ArrayType; diff --git a/test-d/omit-deep.ts b/test-d/omit-deep.ts index 21521a394..de5f6dd46 100644 --- a/test-d/omit-deep.ts +++ b/test-d/omit-deep.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import type {OmitDeep} from '../index'; +import type {OmitDeep, Simplify} from '../index'; declare class ClassA { a: string; @@ -37,6 +37,18 @@ expectType>(normal); declare const normal2: OmitDeep; expectType<{object: Omit}>(normal2); +declare const omitNotExistProperty: OmitDeep; +expectType(omitNotExistProperty); + +declare const omitNotExistProperties: OmitDeep; +expectType(omitNotExistProperties); + +declare const omitNotExistProperty2: OmitDeep; +expectType(omitNotExistProperty2); + +declare const omitNotExistArrayProperty2: OmitDeep<[1, 2, 3], 'not_in_array'>; +expectType<[1, 2, 3]>(omitNotExistArrayProperty2); + declare const number: OmitDeep; expectType<{object: Omit}>(number);