-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
to support any number of keys
- Loading branch information
Showing
4 changed files
with
83 additions
and
59 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
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
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,66 +1,70 @@ | ||
import { assertType, canAssign, Equal, Pick, pick } from '../index.js' | ||
|
||
test('pick properties from object', () => { | ||
const actual = pick({ a: 1, b: 2 }, 'a') | ||
describe(`${pick.name}()`, () => { | ||
it('picks properties from object', () => { | ||
const actual = pick({ a: 1, b: 2 }, 'a') | ||
|
||
expect(actual).toEqual({ a: 1 }) | ||
expect(actual).toEqual({ a: 1 }) | ||
}) | ||
}) | ||
|
||
test('distributive pick', () => { | ||
type Action = InvokeAction | ReturnAction | ||
describe(`Pick<T, K>`, () => { | ||
test('distributive pick', () => { | ||
type Action = InvokeAction | ReturnAction | ||
|
||
type InvokeAction = { | ||
type: 'invoke', | ||
id: string, | ||
payload: string[], | ||
} | ||
type InvokeAction = { | ||
type: 'invoke', | ||
id: string, | ||
payload: string[], | ||
} | ||
|
||
type ReturnAction = { | ||
type: 'return', | ||
id: string, | ||
payload: string, | ||
} | ||
type ReturnAction = { | ||
type: 'return', | ||
id: string, | ||
payload: string, | ||
} | ||
|
||
const x: Pick<Action, 'type' | 'payload'> = { type: 'invoke', payload: [] } | ||
const x: Pick<Action, 'type' | 'payload'> = { type: 'invoke', payload: [] } | ||
|
||
const actions: Action[] = [] | ||
const actions: Action[] = [] | ||
|
||
actions.push({ ...x, id: '1' }) | ||
}) | ||
actions.push({ ...x, id: '1' }) | ||
}) | ||
|
||
test('distributive pick with disjoined keys', () => { | ||
type Union = { | ||
type: 'A', | ||
foo: string, | ||
} | { | ||
type: 'B', | ||
foo: string, | ||
bar: string, | ||
} | ||
type Id<T> = { [P in keyof T]: T[P] } | ||
let x: Id<Pick<Union, 'type' | 'bar'>> = { type: 'A' } | ||
x = { type: 'B', bar: 'bar' } | ||
test('distributive pick with disjoined keys', () => { | ||
type Union = { | ||
type: 'A', | ||
foo: string, | ||
} | { | ||
type: 'B', | ||
foo: string, | ||
bar: string, | ||
} | ||
type Id<T> = { [P in keyof T]: T[P] } | ||
let x: Id<Pick<Union, 'type' | 'bar'>> = { type: 'A' } | ||
x = { type: 'B', bar: 'bar' } | ||
|
||
expect(x.bar).toBe('bar') | ||
}) | ||
expect(x.bar).toBe('bar') | ||
}) | ||
|
||
test('intersection types with generic', () => { | ||
type Foo = { a: string, b: string } | ||
function foo<T>(input: Pick<Foo & T, 'a'>): void { | ||
assertType.isString(input.a) | ||
} | ||
foo({ a: '1' }) | ||
}) | ||
test('intersection types with generic', () => { | ||
type Foo = { a: string, b: string } | ||
function foo<T>(input: Pick<Foo & T, 'a'>): void { | ||
assertType.isString(input.a) | ||
} | ||
foo({ a: '1' }) | ||
}) | ||
|
||
test('optional property remains optional', () => { | ||
type Foo = { a?: string, b: string } | ||
type A = Pick<Foo, 'a'> | ||
assertType.isTrue(canAssign<A>()({})) | ||
}) | ||
test('optional property remains optional', () => { | ||
type Foo = { a?: string, b: string } | ||
type A = Pick<Foo, 'a'> | ||
assertType.isTrue(canAssign<A>()({})) | ||
}) | ||
|
||
test('pick never gets empty object', () => { | ||
type A = { a: number } | ||
type S = Pick<A, never> | ||
type K = keyof S | ||
assertType.isTrue(true as Equal<K, never>) | ||
test('pick never gets empty object', () => { | ||
type A = { a: number } | ||
type S = Pick<A, never> | ||
type K = keyof S | ||
assertType.isTrue(true as Equal<K, never>) | ||
}) | ||
}) |
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