Skip to content

Commit

Permalink
same for omit
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller committed Jan 20, 2024
1 parent a9bb20d commit 26e31cd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions test/omit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ expectType<{ foo: number; bar: string; biz?: boolean; }>(omit([])(obj));
expectType<{ bar: string; biz?: boolean }>(omit(['foo'])(obj));
expectType<{ biz?: boolean }>(omit(['foo', 'bar'])(obj));
expectType<{}>(omit(['foo', 'bar', 'biz'])(obj));
// as long as some of the array has valid keys, it will accept it, but will return `never` if there are any invalid keys
expectType<never>(omit(['baz', 'bar', 'biz'])(obj));
// if the array is only invalid keys, it will error
// errors with `never` if array contains some but not all keys on obj
expectError(omit(['baz', 'bar', 'biz'])(obj));
// errors (with better message) with array of all unknown keys
expectError(omit(['baz'])(obj));
// make sure typed array works
expectType<{}>(omit([] as (keyof typeof obj)[])(obj));
Expand All @@ -26,4 +26,5 @@ expectError(omit(['baz', 'bar', 'biz'], obj));
expectType<{}>(omit([] as (keyof typeof obj)[], obj));

// Record
expectType<Record<string, number>>(omit(['foo', 'bar'])({} as Record<string, number>));
expectType<Record<string, number>>(omit(['foo', 'bar'], {} as Record<string, number>));
3 changes: 2 additions & 1 deletion test/pick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ expectType<{ foo: number; bar: string; }>(pick(['foo', 'bar'])(obj));
expectType<{ foo: number; bar: string; biz?: boolean }>(pick(['foo', 'bar', 'biz'])(obj));
// as long as some of the array has valid keys, it will accept it, but will return `never` if there are any invalid keys
expectError(pick(['baz', 'bar', 'biz'])(obj));
// if the array is only invalid keys, it will error
// errors (with better message) with array of all unknown keys
expectError(pick(['baz'])(obj));
// make sure typed array works
expectType<typeof obj>(pick([] as (keyof typeof obj)[])(obj));
Expand All @@ -26,4 +26,5 @@ expectError(pick(['baz', 'bar', 'biz'], obj));
expectType<typeof obj>(pick([] as (keyof typeof obj)[], obj));

// Record
expectType<Record<string, number>>(pick(['foo', 'bar'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar'], {} as Record<string, number>));
2 changes: 1 addition & 1 deletion types/omit.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElementOf } from './util/tools';

export function omit<const Names extends readonly PropertyKey[]>(names: Names): <U extends Partial<Record<ElementOf<Names>, any>>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : ElementOf<Names> extends keyof U ? Omit<U, ElementOf<Names>> : never;
export function omit<const Keys extends readonly PropertyKey[]>(names: Keys): <U extends Partial<Record<ElementOf<Keys>, any>>>(obj: ElementOf<Keys> extends keyof U ? U : never) => ElementOf<Keys> extends keyof U ? Omit<U, ElementOf<Keys>> : never;
export function omit<U, const Names extends readonly (keyof U)[]>(names: Names, obj: U): string extends keyof U ? Record<string, U[keyof U]> : Omit<U, ElementOf<Names>>;

0 comments on commit 26e31cd

Please sign in to comment.