redux-devtools/packages/redux-devtools-extension/src/utils/assign.ts
Daniel Rosenwasser 41ea59b42e
Provide explicit constraints for TypeScript 4.8 (#1201)
* Add `extends object` constraint to `T` in `assign` helper.

* Add `JSX.IntrinsictAttributes` constraint to `P` in `Tabs`.

* Format

Co-authored-by: Nathan Bierema <nbierema@gmail.com>
2022-07-20 00:25:45 +00:00

27 lines
541 B
TypeScript

const objectKeys =
Object.keys ||
function (obj) {
const keys = [];
for (const key in obj) {
if ({}.hasOwnProperty.call(obj, key)) keys.push(key);
}
return keys;
};
export default function assign<T extends object, K extends keyof T>(
obj: T,
newKey: K,
newValue: T[K]
): T {
const keys = objectKeys(obj);
const copy: T = {} as T;
for (let i = 0, l = keys.length; i < l; i++) {
const key = keys[i];
copy[key as keyof T] = obj[key as keyof T];
}
copy[newKey] = newValue;
return copy;
}