Use an UnknownObject type alias

This commit is contained in:
Scott Trinh 2022-05-19 09:13:56 -04:00 committed by Orion Henry
parent fd02585d2a
commit d2fba6bf04
2 changed files with 7 additions and 3 deletions
automerge-js/src

View file

@ -1,4 +1,6 @@
export function isObject(obj: any) : boolean {
import { UnknownObject } from './types';
export function isObject(obj: unknown) : obj is UnknownObject {
return typeof obj === 'object' && obj !== null
}
@ -6,9 +8,9 @@ export function isObject(obj: any) : boolean {
* Returns a shallow copy of the object `obj`. Faster than `Object.assign({}, obj)`.
* https://jsperf.com/cloning-large-objects/1
*/
export function copyObject(obj: any) : any {
export function copyObject<T extends UnknownObject>(obj: T) : T {
if (!isObject(obj)) return {}
const copy : any = {}
const copy = {}
for (const key of Object.keys(obj)) {
copy[key] = obj[key]
}

View file

@ -0,0 +1,2 @@
export type UnknownObject = Record<string | number | symbol, unknown>;
export type Dictionary<T> = Record<string, T>;