Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OmitDeep: fix handling if path not matched #834

Merged
merged 1 commit into from Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions 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';
Expand Down Expand Up @@ -69,7 +71,7 @@ type AddressInfo = OmitDeep<Info1, 'address.1.foo'>;
@category Object
@category Array
*/
export type OmitDeep<T, PathUnion extends Paths<T>> =
export type OmitDeep<T, PathUnion extends LiteralUnion<Paths<T>, string>> =
SimplifyDeep<
SharedUnionFieldsDeep<
{[P in PathUnion]: OmitDeepWithOnePath<T, P>}[PathUnion]
Expand Down Expand Up @@ -102,9 +104,11 @@ P extends `${infer RecordKeyInPath}.${infer SubPath}`
: ObjectT[Key]
}
: ExactKey<ObjectT, P> extends infer Key
? Key extends PropertyKey
? Omit<ObjectT, Key>
: ObjectT
? IsNever<Key> extends true
? ObjectT
: Key extends PropertyKey
? Omit<ObjectT, Key>
: ObjectT
: ObjectT;

/**
Expand Down Expand Up @@ -133,4 +137,4 @@ type OmitDeepArrayWithOnePath<ArrayType extends UnknownArray, P extends string |
? []
// If `ArrayIndex` is a number literal
: ArraySplice<ArrayType, ArrayIndex, 1, [unknown]>
: never;
: ArrayType;
14 changes: 13 additions & 1 deletion 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;
Expand Down Expand Up @@ -37,6 +37,18 @@ expectType<Omit<Testing, 'object'>>(normal);
declare const normal2: OmitDeep<Testing, 'object.string'>;
expectType<{object: Omit<BaseType, 'string'>}>(normal2);

declare const omitNotExistProperty: OmitDeep<Testing, 'not_in_Testing'>;
expectType<Testing>(omitNotExistProperty);

declare const omitNotExistProperties: OmitDeep<Testing, 'not_in_Testing' | 'not_in_Testing2'>;
expectType<Testing>(omitNotExistProperties);

declare const omitNotExistProperty2: OmitDeep<Testing, 'object.not_in_object'>;
expectType<Testing>(omitNotExistProperty2);

declare const omitNotExistArrayProperty2: OmitDeep<[1, 2, 3], 'not_in_array'>;
expectType<[1, 2, 3]>(omitNotExistArrayProperty2);

declare const number: OmitDeep<Testing, 'object.number'>;
expectType<{object: Omit<BaseType, 'number'>}>(number);

Expand Down