Skip to content

Commit 783e6f0

Browse files
fix: loosen stable metadata default schema types (#1221)
Co-authored-by: Subharup Nandi <Subharup-31@users.noreply.github.com> Co-authored-by: Edmund Hung <edmund@cloudflare.com>
1 parent 08fe6b0 commit 783e6f0

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@conform-to/react': patch
3+
---
4+
5+
Fix stable metadata default schema types so typed metadata is assignable to the default metadata aliases.

packages/conform-react/context.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export type Metadata<
6464
};
6565

6666
export type FormMetadata<
67-
Schema extends Record<string, unknown> = Record<string, unknown>,
67+
Schema extends Record<string, unknown> = any,
6868
FormError = string[],
6969
> = Omit<Metadata<Schema, Schema, FormError>, 'id'> &
7070
Pick<FormContext<Schema, FormError>, Intent['type']> & {
@@ -106,7 +106,7 @@ type SubfieldMetadata<
106106

107107
export type FieldMetadata<
108108
Schema = unknown,
109-
FormSchema extends Record<string, any> = Record<string, unknown>,
109+
FormSchema extends Record<string, any> = any,
110110
FormError = string[],
111111
> = Metadata<Schema, FormSchema, FormError> &
112112
Constraint & { formId: FormId<FormSchema, FormError> } & SubfieldMetadata<

packages/conform-react/hooks.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ export function useForm<
9696
});
9797
const stateSnapshot = useFormState(context, subjectRef);
9898
const noValidate = useNoValidate(options.defaultNoValidate);
99-
const form = getFormMetadata(context, subjectRef, stateSnapshot, noValidate);
99+
const form = getFormMetadata<Schema, FormError, FormValue>(
100+
context,
101+
subjectRef,
102+
stateSnapshot,
103+
noValidate,
104+
);
100105

101106
useEffect(() => {
102107
context.runSideEffect(stateSnapshot.pendingIntents);
@@ -119,12 +124,17 @@ export function useFormMetadata<
119124
const stateSnapshot = useFormState(context, subjectRef);
120125
const noValidate = useNoValidate(options.defaultNoValidate);
121126

122-
return getFormMetadata(context, subjectRef, stateSnapshot, noValidate);
127+
return getFormMetadata<Schema, FormError, unknown>(
128+
context,
129+
subjectRef,
130+
stateSnapshot,
131+
noValidate,
132+
);
123133
}
124134

125135
export function useField<
126136
FieldSchema,
127-
FormSchema extends Record<string, unknown> = Record<string, unknown>,
137+
FormSchema extends Record<string, unknown> = any,
128138
FormError = string[],
129139
>(
130140
name: FieldName<FieldSchema, FormSchema, FormError>,
@@ -144,7 +154,12 @@ export function useField<
144154
stateSnapshot,
145155
name,
146156
);
147-
const form = getFormMetadata(context, subjectRef, stateSnapshot, false);
157+
const form = getFormMetadata<FormSchema, FormError, unknown>(
158+
context,
159+
subjectRef,
160+
stateSnapshot,
161+
false,
162+
);
148163

149164
return [field, form];
150165
}

playground/app/routes/typing.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ function isFieldMetadataType3<
8585
export default function Example() {
8686
const [form, fields] = useForm<Schema>({});
8787

88-
// @ts-expect-error https://github.com/edmundhung/conform/issues/406
8988
isFormMetadataType0(form);
9089
isFormMetadataType1<Schema>(form);
9190
isFormMetadataType2<Schema, string[]>(form);

0 commit comments

Comments
 (0)