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
5 changes: 5 additions & 0 deletions .changeset/strong-pandas-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@conform-to/react': minor
---

Fix `DefaultValue<Shape>` to expose `undefined` when `Shape` is a generic type parameter, so wrappers work with `exactOptionalPropertyTypes`.
14 changes: 8 additions & 6 deletions packages/conform-react/future/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,17 @@ export type UseFormDataOptions<Value = undefined> = {
};

export type DefaultValue<Shape> =
Shape extends Record<string, any>
? { [Key in keyof Shape]?: DefaultValue<Shape[Key]> } | null | undefined
| null
| undefined
| (Shape extends Record<string, any>
? { [Key in keyof Shape]?: DefaultValue<Shape[Key]> }
: Shape extends Array<infer Item>
? Array<DefaultValue<Item>> | null | undefined
? Array<DefaultValue<Item>>
: Shape extends File | File[]
? null | undefined // We don't support setting default value for file inputs yet
? never // We don't support setting default value for file inputs yet
: Shape extends string | null | undefined
? Shape | null | undefined
: Shape | string | null | undefined;
? Shape
: Shape | string);

export type FormState<
ErrorShape = any,
Expand Down
7 changes: 7 additions & 0 deletions packages/conform-react/tests/types.test-d.ts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is very concise and can be read as:

Is null visible in DefaultValue<Shape> without reducing the conditional?

L317: If you have a wrapper function GenericDefault that returns a DefaultValue<Shape>, then

L319: any function of signature () => null matches the type GenericDefault. Reason: null must be assignable to DefaultValue<S> for every S the caller chooses, and returning null satisfies that for every Shape.

L320: Same argument, but with undefined.

The fact that the previous definition of DefaultValue fails this test but this PR's definition doesn't, implies that hoisting helps TypeScript prove the correctness of more cases. It also follows an algebraic transformation that looks like factoring out a value from a sum, basically applying the distributive law over sum types:

(A × C) + (B × C) = (A + B) × C

Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ test('literal default values in intents', () => {
assertType<unknown>(metadata.defaultPayload);
});

test('DefaultValue with a generic shape', () => {
type GenericDefault = <Shape>() => DefaultValue<Shape>;

assertType<GenericDefault>(() => null);
assertType<GenericDefault>(() => undefined);
});

test('FormOptions', () => {
type TestSchema = { name: string; email: string };

Expand Down
Loading