Skip to content

Commit

Permalink
fix: Better types for assignKeys utility (#23496)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Jul 21, 2023
1 parent a89e307 commit f049774
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
31 changes: 24 additions & 7 deletions lib/util/assign-keys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ import { assignKeys } from './assign-keys';

describe('util/assign-keys', () => {
it('should assign values from right to left for specified keys', () => {
type Left = { a: number; b: number };
const left: Left = { a: 1, b: 2 };
type Left = {
foo: number | string;
bar: number | boolean;
baz?: number;
};
const left: Left = {
foo: 'foo',
bar: false,
baz: 42,
};

type Right = { a?: number; b?: number; c?: number };
const right: Right = { a: 3, c: 4 };
type Right = {
foo?: number;
bar?: number;
baz?: number;
};
const right: Right = {
foo: 1,
bar: 2,
baz: 3,
};

const result = assignKeys(left, right, ['a', 'b']);
const result = assignKeys(left, right, ['foo', 'bar']);
expect(result).toEqual({
a: 3,
b: 2,
foo: 1,
bar: 2,
baz: 42,
});
expect(result).toBe(left);
});
Expand Down
4 changes: 2 additions & 2 deletions lib/util/assign-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import is from '@sindresorhus/is';
* Assigns non-nullish values from `right` to `left` for the given `keys`.
*/
export function assignKeys<
Left extends { [key in K]?: Right[key] },
Right extends { [key in K]?: any },
Left extends { [key in K]?: unknown },
Right extends { [key in K]?: Left[key] },
K extends keyof Right
>(left: Left, right: Right, keys: K[]): Left {
for (const key of keys) {
Expand Down

0 comments on commit f049774

Please sign in to comment.