Skip to content

Commit

Permalink
fix: add typing to allow for non-objects input args (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Aug 8, 2022
1 parent c912d88 commit 1f3a701
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Expand Up @@ -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
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
15 changes: 8 additions & 7 deletions src/types.ts
@@ -1,11 +1,12 @@
type Input = Record<string | number | symbol, any>
type IgnoredInput = boolean | number | null | any[] | undefined

export type Merger = <T extends Input, K extends keyof T>(
obj: T,
key: keyof T,
value: T[K],
namespace: string
) => any;
) => any

type nullish = null | undefined | void

Expand All @@ -27,16 +28,16 @@ type MergeObjects<
export type DefuFn = <Source extends Input, Defaults extends Input>(
source: Source,
...defaults: Defaults[]
) => MergeObjects<Source, Defaults>;
) => MergeObjects<Source, Defaults>

export interface Defu {
<Source extends Input, Defaults extends Input>(source: Source, ...defaults: Defaults[]): MergeObjects<
<Source extends Input, Defaults extends Input>(source: Source | IgnoredInput, ...defaults: Array<Defaults | IgnoredInput>): MergeObjects<
Source,
Defaults
>;
fn: DefuFn;
arrayFn: DefuFn;
extend(merger?: Merger): DefuFn;
>
fn: DefuFn
arrayFn: DefuFn
extend(merger?: Merger): DefuFn
}

type MergeArrays<Destination, Source> = Destination extends Array<infer DestinationType>
Expand Down
3 changes: 0 additions & 3 deletions test/defu.test.ts
Expand Up @@ -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 })
}
})
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Expand Up @@ -2,9 +2,12 @@
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"skipLibCheck": true,
"declaration": true
},
"include": [
"src"
"src",
"test"
]
}

0 comments on commit 1f3a701

Please sign in to comment.