-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f81c75a
commit 3f8e31d
Showing
3 changed files
with
43 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expectType, expectError } from 'tsd'; | ||
|
||
import { pick, KeysAsTuple } from '../es'; | ||
|
||
const obj = { foo: 1, bar: '2', biz: false }; | ||
|
||
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)); | ||
|
||
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)); | ||
|
||
// Record | ||
expectType<Record<string, number>>(pick(['foo', 'bar'], {} as Record<string, number>)); | ||
|
||
const names: string[] = ['foo', 'bar']; | ||
// in cases where names is either `string[]` or `(keyof obj)[]`, cast with supplied `KeysAsTuple` type function | ||
expectType<typeof obj>(pick(names as KeysAsTuple<typeof obj>, obj)); | ||
// this case however is inaccurate, best to cast as a `Partial` in a real-world scenario | ||
expectType<Partial<typeof obj>>(pick(names as KeysAsTuple<typeof obj>, obj) as Partial<typeof obj>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,5 @@ | ||
import * as _ from 'ts-toolbelt'; | ||
import { ElementOf } from './util/tools'; | ||
|
||
export function pick<const Names extends readonly [PropertyKey, ...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, ...(keyof U)[]]>(names: Names, obj: U): string extends keyof U ? Record<string, U[keyof U]> : Pick<U, ElementOf<Names>>; | ||
|
||
export function pick<T extends readonly [any, ...any], K extends string | number | symbol>( | ||
names: readonly K[], | ||
array: T, | ||
): { | ||
[P in K as P extends number | ||
? _.N.Greater<T['length'], P> extends 1 | ||
? P | ||
: never | ||
: never]: P extends keyof T ? T[P] : T[number]; | ||
}; | ||
export function pick<T, K extends string | number | symbol>( | ||
names: readonly K[], | ||
obj: T, | ||
): { [P in keyof T as P extends K ? P : never]: T[P] }; | ||
export function pick<K extends string | number | symbol>( | ||
names: readonly K[], | ||
): <T extends readonly [any, ...any] | object>( | ||
obj: T, | ||
) => T extends readonly [any, ...any] | ||
? { | ||
[P in K as P extends number | ||
? _.N.Greater<T['length'], P> extends 1 | ||
? P | ||
: never | ||
: never]: P extends keyof T ? T[P] : T[number]; | ||
} | ||
: { [P in keyof T as P extends K ? P : never]: T[P] }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters