From fc148a41b12f2b6781673ccff62dc4fd5f1db960 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 5 Aug 2022 22:07:19 +0100 Subject: [PATCH 1/3] fix: add typing to allow for non-objects --- .github/workflows/ci.yml | 1 + package.json | 3 ++- src/types.ts | 4 +++- test/defu.test.ts | 3 --- tsconfig.json | 5 ++++- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1566079..d704c05 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,5 +21,6 @@ jobs: - run: pnpm install - run: pnpm lint - run: pnpm build + - run: pnpm test:types - run: pnpm vitest --coverage - uses: codecov/codecov-action@v2 diff --git a/package.json b/package.json index 6696e3f..b6d744c 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "lint": "eslint --ext .ts src", "prepack": "pnpm build", "release": "pnpm test && standard-version && git push --follow-tags && pnpm publish", - "test": "pnpm lint && pnpm vitest" + "test": "pnpm lint && pnpm vitest", + "test:types": "tsc --noEmit" }, "devDependencies": { "@nuxtjs/eslint-config-typescript": "latest", diff --git a/src/types.ts b/src/types.ts index d372eae..2b2cdd2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -29,8 +29,10 @@ export type DefuFn = ( ...defaults: Defaults[] ) => MergeObjects; +type HandledNonObject = boolean | number | null | any[] | undefined + export interface Defu { - (source: Source, ...defaults: Defaults[]): MergeObjects< + (source: Source | HandledNonObject, ...defaults: Array): MergeObjects< Source, Defaults >; diff --git a/test/defu.test.ts b/test/defu.test.ts index 876c8b4..8ae5751 100644 --- a/test/defu.test.ts +++ b/test/defu.test.ts @@ -58,14 +58,12 @@ describe('defu', () => { it('should handle non object first param', () => { for (const val of nonObject) { - // @ts-expect-error expect(defu(val, { d: true })).toEqual({ d: true }) } }) it('should handle non object second param', () => { for (const val of nonObject) { - // @ts-expect-error expect(defu({ d: true }, val)).toEqual({ d: true }) } }) @@ -92,7 +90,6 @@ describe('defu', () => { }) it('should ignore non-object arguments', () => { - // @ts-expect-error expect(defu(null, { foo: 1 }, false, 123, { bar: 2 })).toEqual({ foo: 1, bar: 2 diff --git a/tsconfig.json b/tsconfig.json index 4a2fa1c..f86f466 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,9 +2,12 @@ "compilerOptions": { "target": "ESNext", "module": "ESNext", + "moduleResolution": "node", + "skipLibCheck": true, "declaration": true }, "include": [ - "src" + "src", + "test" ] } From f68780b9446aa15378227ad3f4f9d24d14b6017a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 13:15:42 +0100 Subject: [PATCH 2/3] refactor: rename to `IgnoredInput` --- src/types.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/types.ts b/src/types.ts index 2b2cdd2..730028a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ type Input = Record +type IgnoredInput = boolean | number | null | any[] | undefined export type Merger = ( obj: T, @@ -29,10 +30,8 @@ export type DefuFn = ( ...defaults: Defaults[] ) => MergeObjects; -type HandledNonObject = boolean | number | null | any[] | undefined - export interface Defu { - (source: Source | HandledNonObject, ...defaults: Array): MergeObjects< + (source: Source | IgnoredInput, ...defaults: Array): MergeObjects< Source, Defaults >; From a3c595014c95dbbcfc1f1dba002f55bcf484a0dd Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 13:16:52 +0100 Subject: [PATCH 3/3] style: remove semis --- src/types.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/types.ts b/src/types.ts index 730028a..5b3da44 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,7 @@ export type Merger = ( key: keyof T, value: T[K], namespace: string -) => any; +) => any type nullish = null | undefined | void @@ -28,16 +28,16 @@ type MergeObjects< export type DefuFn = ( source: Source, ...defaults: Defaults[] -) => MergeObjects; +) => MergeObjects export interface Defu { (source: Source | IgnoredInput, ...defaults: Array): MergeObjects< Source, Defaults - >; - fn: DefuFn; - arrayFn: DefuFn; - extend(merger?: Merger): DefuFn; + > + fn: DefuFn + arrayFn: DefuFn + extend(merger?: Merger): DefuFn } type MergeArrays = Destination extends Array