Use an UnknownObject
type alias
This commit is contained in:
parent
fd02585d2a
commit
d2fba6bf04
2 changed files with 7 additions and 3 deletions
|
@ -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]
|
||||
}
|
||||
|
|
2
automerge-js/src/types.ts
Normal file
2
automerge-js/src/types.ts
Normal file
|
@ -0,0 +1,2 @@
|
|||
export type UnknownObject = Record<string | number | symbol, unknown>;
|
||||
export type Dictionary<T> = Record<string, T>;
|
Loading…
Add table
Reference in a new issue