diff --git a/index.d.ts b/index.d.ts index 7412fcf..aa2807c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,50 @@ -declare function deepmerge(x: Partial, y: Partial, options?: deepmerge.Options): T; -declare function deepmerge(x: Partial, y: Partial, options?: deepmerge.Options): T1 & T2; +/** + * object skin copy + * e.g: SkinMerge<{ a: 1, c: 1 }, { a: 2, b: 2 }> ==> { a: 2, b: 2, c: 1 } + */ +type SkinMerge = { + [k in (keyof O1 | keyof O2)]: k extends keyof O2 ? O2[k] : (k extends keyof O1 ? O1[k] : never) +} + +/** + * get keys from O where the value is object + * e.g: ObjKeys<{ a: {}, b: {}, c: '1' }> ==> 'a' | 'b' + */ +type ObjKeys = ({ + [k in keyof O]: O[k] extends object ? k : never; +})[keyof O]; + +/** + * contrary to ObjKeys, e.g: NotObjKeys<{ a: {}, b: {}, c: '1' }> ==> 'c' + */ +type NotObjKeys = Exclude>; + +/** + * use NotObjKeys to contruct object type from 'O' + * e.g: PickNotObj<{ a: 'a', b: 'b', c: {} }> ==> { a: 'a', b: 'b' } + */ +type PickNotObj = Pick>; + +/** + * Merge Deep Recursively For O1 O2 + * github gist about MergeDeep: https://gist.github.com/eczn/f037be1fa3650304c5c4b80c3b9e18a7 + */ +type MergeDeep = SkinMerge, PickNotObj> & { + // k1 is not in ObjKeys + [k1 in Exclude, ObjKeys>]: O1[k1] +} & { + // k2 is ObjKeys, maybe some ObjKeys alson in there, so we should find it out + [k2 in ObjKeys]: k2 extends ObjKeys ? MergeDeep : O2[k2] +} + +/** + * ## JS Object Deep Merge + * look more info in https://github.com/TehShrike/deepmerge + * @param x the first obj + * @param y the second obj + * @param options deepmerge option + */ +declare function deepmerge(x: T1, y: T2, options?: deepmerge.Options): MergeDeep declare namespace deepmerge { export interface Options { diff --git a/package-lock.json b/package-lock.json index c112ccb..2f64d16 100644 --- a/package-lock.json +++ b/package-lock.json @@ -644,9 +644,9 @@ } }, "typescript": { - "version": "2.2.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.2.2.tgz", - "integrity": "sha1-YGAiUIR5tV/6NotY/uljoD39eww=", + "version": "3.7.2", + "resolved": "http://r.tnpm.oa.com/typescript/download/typescript-3.7.2.tgz", + "integrity": "sha1-J+SJuV+lkJRF6f717kjYFpetGPs=", "dev": true }, "uglify-js": { diff --git a/package.json b/package.json index 99ed0d6..35a10a7 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "rollup-plugin-node-resolve": "^5.2.0", "tape": "^4.11.0", "ts-node": "7.0.1", - "typescript": "=2.2.2", + "typescript": "^3.7.2", "uglify-js": "^3.6.1" }, "license": "MIT",