Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export type NoInfer<T> = [T][T extends any ? 0 : never];
/**
* Adapted from type-fest's PartialDeep
*/
export type PartialDeep<T> = T extends (...args: any[]) => any
? PartialDeepObject<T> | undefined
export type PartialDeep<T> = T extends (...args: infer P) => infer R
? ((...args: P) => PartialDeep<R> | void) & PartialDeepObject<T> | PartialDeepObject<T> | undefined
: T extends object
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
? ItemType[] extends T // Test for arrays (non-tuples) specifically
Expand Down
17 changes: 17 additions & 0 deletions tests/fromPartial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ describe("fromPartial", () => {
it("Should accept functions", () => {
accept<() => void>(fromPartial({}));
});

it("Should deeply infer types from functions", () => {
accept<(foo: string) => (bar: number) => {baz: string}>(fromPartial((foo) => (bar) => {}));
});

it("Should accept interfaces which combine object properties and a call signature", () => {
accept<{
Expand All @@ -89,4 +93,17 @@ describe("fromPartial", () => {
}),
);
});

it("Should handle deeply recursive partial types", () => {
interface RecursiveInterface {
(bar: string) : RecursiveInterface;
foo: {
deepProperty: string
},
}
accept<RecursiveInterface>(
fromPartial(bar => (bar2)=> ({})),
);
});

});