From fa6e31b70fa52c754708c90a3d5fd83ab3de5a94 Mon Sep 17 00:00:00 2001 From: Som Shekhar Mukherjee <49264891+som-sm@users.noreply.github.com> Date: Sun, 9 Feb 2025 14:28:53 +0530 Subject: [PATCH] `OmitDeep`: Fix removal of multiple paths within arrays (#1049) --- source/omit-deep.d.ts | 16 ++++++++++++---- test-d/omit-deep.ts | 6 ++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/source/omit-deep.d.ts b/source/omit-deep.d.ts index 72ec6445..b1a20622 100644 --- a/source/omit-deep.d.ts +++ b/source/omit-deep.d.ts @@ -1,10 +1,10 @@ +import type {UnionToTuple} from 'expect-type'; 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 {Paths} from './paths'; -import type {SharedUnionFieldsDeep} from './shared-union-fields-deep'; import type {SimplifyDeep} from './simplify-deep'; import type {UnknownArray} from './unknown-array'; @@ -92,11 +92,19 @@ type AddressInfo = OmitDeep; */ export type OmitDeep, string>> = SimplifyDeep< - SharedUnionFieldsDeep< - {[P in PathUnion]: OmitDeepWithOnePath}[PathUnion] - >, + OmitDeepHelper>, UnknownArray>; +/** +Internal helper for {@link OmitDeep}. + +Recursively transforms `T` by applying {@link OmitDeepWithOnePath} for each path in `PathTuple`. +*/ +type OmitDeepHelper = + PathTuple extends [infer Path, ...infer RestPaths] + ? OmitDeepHelper, RestPaths> + : T; + /** Omit one path from the given object/array. */ diff --git a/test-d/omit-deep.ts b/test-d/omit-deep.ts index 1b241e68..47421a75 100644 --- a/test-d/omit-deep.ts +++ b/test-d/omit-deep.ts @@ -135,3 +135,9 @@ expectType<{array: [ declare const tuple: OmitDeep<{array: BaseType['tuples']}, 'array.0'>; expectType<{array: [unknown, 'bar']}>(tuple); + +declare const arrayWithMultiplePaths: OmitDeep<{array: Array<{a: string; b: number; c: string}>}, `array.${number}.a` | `array.${number}.b`>; +expectType<{array: Array<{c: string}>}>(arrayWithMultiplePaths); + +declare const tupleWithMultiplePaths: OmitDeep<{tuple: [{a: string; b: number; c: string}]}, 'tuple.0.a' | 'tuple.0.b'>; +expectType<{tuple: [{c: string}]}>(tupleWithMultiplePaths);