Skip to content

Commit

Permalink
fix pick types to allow interfaces (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray authored Jan 16, 2023
1 parent 40b2059 commit 766347e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const listify = <TValue, TKey extends string | number | symbol, KResult>(
* Pick a list of properties from an object
* into a new object
*/
export const pick = <T extends Record<string, unknown>, TKeys extends keyof T>(
export const pick = <T extends object, TKeys extends keyof T>(
obj: T,
keys: TKeys[]
): Pick<T, TKeys> => {
Expand Down
20 changes: 18 additions & 2 deletions src/tests/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ describe('object module', () => {
assert.deepEqual(result, {})
})
test('handle key not in object', () => {
const result = _.pick({ a: 2, b: 3 } as any, ['c'])
const result = _.pick({ a: 2, b: 3 }, ['c'] as any)
assert.deepEqual(result, {} as any)
})
test('handle one key not in object', () => {
const result = _.pick({ a: 2, b: 3 } as any, ['a', 'c'])
const result = _.pick({ a: 2, b: 3 }, ['a', 'c'] as any)
assert.deepEqual(result, { a: 2 } as any)
})
test('does not ignore undefined values', () => {
Expand All @@ -230,6 +230,22 @@ describe('object module', () => {
a: 2
})
})
test('type: accepts an interface', () => {
interface SomeDeclaredType {
a: string
b: Error
c: number[]
}
const x: SomeDeclaredType = {
a: 'alpha',
b: new Error('beta'),
c: [3]
}
const result = _.pick(x, ['a'])
assert.deepEqual(result, {
a: 'alpha'
})
})
})

describe('omit function', () => {
Expand Down

1 comment on commit 766347e

@vercel
Copy link

@vercel vercel bot commented on 766347e Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.