From 1f3a701bc3fd839344359ad5c2b358fbefd978cc Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Aug 2022 13:45:03 +0100 Subject: [PATCH] fix: add typing to allow for non-objects input args (#42) --- .github/workflows/ci.yml | 1 + package.json | 3 ++- src/types.ts | 15 ++++++++------- test/defu.test.ts | 3 --- tsconfig.json | 5 ++++- 5 files changed, 15 insertions(+), 12 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..5b3da44 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,11 +1,12 @@ type Input = Record +type IgnoredInput = boolean | number | null | any[] | undefined export type Merger = ( obj: T, key: keyof T, value: T[K], namespace: string -) => any; +) => any type nullish = null | undefined | void @@ -27,16 +28,16 @@ type MergeObjects< export type DefuFn = ( source: Source, ...defaults: Defaults[] -) => MergeObjects; +) => MergeObjects export interface Defu { - (source: Source, ...defaults: Defaults[]): MergeObjects< + (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 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" ] }