OmitDeep: Fix removal of multiple paths within arrays (#1049)

This commit is contained in:
Som Shekhar Mukherjee 2025-02-09 14:28:53 +05:30 committed by GitHub
parent 9aba4c33c8
commit fa6e31b70f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 4 deletions

16
source/omit-deep.d.ts vendored
View File

@ -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<Info1, 'address.1.foo'>;
*/
export type OmitDeep<T, PathUnion extends LiteralUnion<Paths<T>, string>> =
SimplifyDeep<
SharedUnionFieldsDeep<
{[P in PathUnion]: OmitDeepWithOnePath<T, P>}[PathUnion]
>,
OmitDeepHelper<T, UnionToTuple<PathUnion>>,
UnknownArray>;
/**
Internal helper for {@link OmitDeep}.
Recursively transforms `T` by applying {@link OmitDeepWithOnePath} for each path in `PathTuple`.
*/
type OmitDeepHelper<T, PathTuple extends UnknownArray> =
PathTuple extends [infer Path, ...infer RestPaths]
? OmitDeepHelper<OmitDeepWithOnePath<T, Path & (string | number)>, RestPaths>
: T;
/**
Omit one path from the given object/array.
*/

View File

@ -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);