-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created workaround for TypeScript type parameter limitation, started …
…adding tests for types Also added more options for PromisifiedModule type (types still need to be reorganized and thought out)
- Loading branch information
Showing
8 changed files
with
209 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { expectTypeOf, test, describe } from "vitest" | ||
import type { MergeModuleMethods } from "./merge" | ||
import { PromisifiedModule } from "../interfaces" | ||
|
||
describe("MergeModule merges modules", () => { | ||
test("override all without new methods", () => { | ||
type A = { | ||
lorem(): string | ||
testing: { | ||
ipsum(): Promise<void> | ||
} | ||
} | ||
type B = { | ||
lorem(): number | ||
testing: { | ||
ipsum(): void | ||
} | ||
} | ||
type Merged = MergeModuleMethods<A, B> | ||
expectTypeOf<Merged>().toMatchTypeOf<B>() | ||
}) | ||
|
||
test("with new methods", () => { | ||
type A = { | ||
what(): void | ||
testing: { | ||
lorem(): string | ||
} | ||
} | ||
type B = { | ||
testing: { | ||
ipsum(): void | ||
} | ||
cool(): boolean | ||
} | ||
type Merged = MergeModuleMethods<A, B> | ||
type Expected = { | ||
what(): void | ||
testing: { | ||
lorem(): string | ||
ipsum(): void | ||
} | ||
cool(): boolean | ||
} | ||
expectTypeOf<Merged>().toMatchTypeOf<Expected>() | ||
}) | ||
|
||
test("without override", () => { | ||
type A = { | ||
what(): void | ||
testing: { | ||
lorem(): string | ||
} | ||
} | ||
type B = never | ||
type Merged = MergeModuleMethods<A, B> | ||
expectTypeOf<Merged>().toMatchTypeOf<A>() | ||
}) | ||
|
||
test("works without base", () => { | ||
type A = never | ||
type B = { | ||
what(): void | ||
} | ||
type Merged = MergeModuleMethods<A, B> | ||
expectTypeOf<Merged>().toMatchTypeOf<B>() | ||
}) | ||
}) | ||
|
||
describe("MergeModule works with PromisifiedModule", () => { | ||
test("With form handling", () => { | ||
type A = PromisifiedModule< | ||
{ | ||
lorem(): Promise<string> | ||
ipsum(): void | ||
}, | ||
true | ||
> | ||
type B = { | ||
lorem(): string | ||
} | ||
type Merged = MergeModuleMethods<A, B> | ||
type Expected = { | ||
lorem(): string | ||
ipsum(): Promise<void> | ||
ipsum(formlike: SubmitEvent | FormData | HTMLFormElement): Promise<void> | ||
} | ||
|
||
expectTypeOf<Merged>().toMatchTypeOf<Expected>() | ||
}) | ||
|
||
test("without form handling", () => { | ||
type A = PromisifiedModule< | ||
{ | ||
lorem(): Promise<string> | ||
test: { | ||
ipsum(): void | ||
} | ||
}, | ||
false | ||
> | ||
type B = { | ||
lorem(): string | ||
} | ||
type Merged = MergeModuleMethods<A, B> | ||
type Expected = { | ||
lorem(): string | ||
test: { | ||
ipsum(): Promise<void> | ||
} | ||
} | ||
expectTypeOf<Merged>().toMatchTypeOf<Expected>() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { AnyFunction } from "../interfaces" | ||
|
||
export type MergeModuleMethods< | ||
ModuleGiven extends object, | ||
Override extends object = never, | ||
Keys extends keyof ModuleGiven = Extract<keyof ModuleGiven, string>, | ||
> = [ModuleGiven] extends [never] | ||
? [Override] extends [never] | ||
? never | ||
: Override | ||
: { | ||
[Key in Keys]: Key extends keyof Override | ||
? ModuleGiven[Key] extends AnyFunction | ||
? Override[Key] extends AnyFunction | ||
? Override[Key] | ||
: ModuleGiven[Key] | ||
: Override[Key] extends object | ||
? ModuleGiven[Key] extends object | ||
? MergeModuleMethods<ModuleGiven[Key], Override[Key]> | ||
: Override[Key] | ||
: Override[Key] | ||
: ModuleGiven[Key] | ||
} & { | ||
[Key in Exclude<keyof Override, Keys>]: Override[Key] | ||
} |