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

fix(pick): update for ts4 support #85

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 56 additions & 20 deletions test/pick.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
import { expectType, expectError } from 'tsd';
import { expectType, expectError, expectAssignable } from 'tsd';

import { pick } from '../es';

const obj = { foo: 1, bar: '2', biz: false };

expectType<{}>(pick([])(obj));
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));
expectError(pick(['baz', 'bar', 'biz'])(obj));
// make sure typed array works
expectType<typeof obj>(pick([] as (keyof typeof obj)[])(obj));

expectType<{}>(pick([], obj));
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));
expectError(pick(['baz', 'bar', 'biz'], obj));
// make sure typed array works
expectType<typeof obj>(pick([] as (keyof typeof obj)[], obj));

// Record
type Obj = { foo: number, bar: string, biz: boolean, baz: Date; a: string; b: string; c: string; d: string, e: string };
const obj = {} as Obj;

expectType<Pick<Obj, never>>(pick([])(obj));
expectType<Pick<Obj, 'foo'>>(pick(['foo'])(obj));
expectType<Pick<Obj, 'foo' | 'bar'>>(pick(['foo', 'bar'])(obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz'>>(pick(['foo', 'bar', 'biz'])(obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz'>>(pick(['foo', 'bar', 'biz', 'baz'])(obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a'>>(pick(['foo', 'bar', 'biz', 'baz', 'a'])(obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a' | 'b'>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b'])(obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a' | 'b' | 'c'>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c'])(obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a' | 'b' | 'c' | 'd'>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd'])(obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a' | 'b' | 'c' | 'd' | 'e'>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd', 'e'] as const)(obj));

// while these technically work, they give back false positives, avoid their use
expectAssignable<Pick<Obj, keyof Obj>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd', 'e'])(obj));
expectType<Pick<Obj, keyof Obj>>(pick([] as (keyof Obj)[])(obj));

expectError(pick(['what'])(obj));

expectType<Pick<Obj, never>>(pick([], obj));
expectType<Pick<Obj, 'foo'>>(pick(['foo'], obj));
expectType<Pick<Obj, 'foo' | 'bar'>>(pick(['foo', 'bar'], obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz'>>(pick(['foo', 'bar', 'biz'], obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz'>>(pick(['foo', 'bar', 'biz', 'baz'], obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a'>>(pick(['foo', 'bar', 'biz', 'baz', 'a'], obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a' | 'b'>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b'], obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a' | 'b' | 'c'>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c'], obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a' | 'b' | 'c' | 'd'>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd'], obj));
expectType<Pick<Obj, 'foo' | 'bar' | 'biz' | 'baz' | 'a' | 'b' | 'c' | 'd' | 'e'>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd', 'e'] as const, obj));

// while this technically work, they give back false positives, avoid their use
expectType<Pick<Obj, keyof Obj>>(pick([] as (keyof Obj)[], obj));

expectError(pick(['what'], obj));

expectType<Record<string, number>>(pick(['foo'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd'])({} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd', 'e'] as const)({} as Record<string, number>));

expectType<Record<string, number>>(pick([], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo'], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar'], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz'], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz'], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a'], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b'], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c'], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd'], {} as Record<string, number>));
expectType<Record<string, number>>(pick(['foo', 'bar', 'biz', 'baz', 'a', 'b', 'c', 'd', 'e'], {} as Record<string, number>));
58 changes: 56 additions & 2 deletions types/pick.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
import { ElementOf } from './util/tools';

export function pick<const Names extends readonly PropertyKey[]>(names: Names): <U extends Record<ElementOf<Names>, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, ElementOf<Names>>;
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<
T1 extends PropertyKey
>(names: [T1]): <U extends Record<T1, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, T1>;
export function pick<
T1 extends PropertyKey,
T2 extends PropertyKey
>(names: [T1, T2]): <U extends Record<T1 | T2, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, T1 | T2>;
export function pick<
T1 extends PropertyKey,
T2 extends PropertyKey,
T3 extends PropertyKey
>(names: [T1, T2, T3]): <U extends Record<T1 | T2 | T3, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, T1 | T2 | T3>;
export function pick<
T1 extends PropertyKey,
T2 extends PropertyKey,
T3 extends PropertyKey,
T4 extends PropertyKey
>(names: [T1, T2, T3, T4]): <U extends Record<T1 | T2 | T3 | T4, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, T1 | T2 | T3 | T4>;
export function pick<
T1 extends PropertyKey,
T2 extends PropertyKey,
T3 extends PropertyKey,
T4 extends PropertyKey,
T5 extends PropertyKey
>(names: [T1, T2, T3, T4, T5]): <U extends Record<T1 | T2 | T3 | T4 | T5, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, T1 | T2 | T3 | T4 | T5>;
export function pick<
T1 extends PropertyKey,
T2 extends PropertyKey,
T3 extends PropertyKey,
T4 extends PropertyKey,
T5 extends PropertyKey,
T6 extends PropertyKey
>(names: [T1, T2, T3, T4, T5, T6]): <U extends Record<T1 | T2 | T3 | T4 | T5 | T6, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, T1 | T2 | T3 | T4 | T5 | T6>;
export function pick<
T1 extends PropertyKey,
T2 extends PropertyKey,
T3 extends PropertyKey,
T4 extends PropertyKey,
T5 extends PropertyKey,
T6 extends PropertyKey,
T7 extends PropertyKey
>(names: [T1, T2, T3, T4, T5, T6, T7]): <U extends Record<T1 | T2 | T3 | T4 | T5 | T6 | T7, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, T1 | T2 | T3 | T4 | T5 | T6 | T7>;
export function pick<
T1 extends PropertyKey,
T2 extends PropertyKey,
T3 extends PropertyKey,
T4 extends PropertyKey,
T5 extends PropertyKey,
T6 extends PropertyKey,
T7 extends PropertyKey,
T8 extends PropertyKey
>(names: [T1, T2, T3, T4, T5, T6, T7, T8]): <U extends Record<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
// back up for over 9, will require `as const` to work
export function pick<Names extends readonly PropertyKey[]>(names: Names): <U extends Record<ElementOf<Names>, any>>(obj: U) => string extends keyof U ? Record<string, U[keyof U]> : Pick<U, ElementOf<Names>>;
// because we can extend (keyof U)(), `names` always gets read as a union of the values, so we don't need to overload these!
export function pick<U, Names extends readonly (keyof U)[]>(names: Names, obj: U): string extends keyof U ? Record<string, U[keyof U]> : Pick<U, ElementOf<Names>>;