Skip to content

Commit

Permalink
better pick
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller committed Jan 21, 2024
1 parent 135c8da commit 709e22f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion test/pick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ expectType<{ foo: number; }>(pick(['foo'])(obj));
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
expectType<never>(pick(['baz', 'bar', 'biz'])(obj));
expectError(pick(['baz', 'bar', 'biz'])(obj));
// if the array is only invalid keys, it will error
expectError(pick(['baz'])(obj));
// make sure typed array works
Expand Down
4 changes: 2 additions & 2 deletions types/pick.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElementOf } from './util/tools';

export function pick<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 ? Pick<U, ElementOf<Names>> : never;
export function pick<U, const Names extends readonly (keyof U)[]>(names: Names, obj: U): string extends keyof U ? Record<string, U[keyof U]> : Pick<U, ElementOf<Names>>;
export function pick<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 ? Pick<U, ElementOf<Keys>> : never;
export function pick<U, Keys extends keyof U>(names: readonly Keys[], obj: U): Pick<U, Keys>;

0 comments on commit 709e22f

Please sign in to comment.