Skip to content

Commit ebccc8b

Browse files
committed
refactor: simplify type inference pattern in docs and examples
1 parent 566e781 commit ebccc8b

File tree

7 files changed

+86
-190
lines changed

7 files changed

+86
-190
lines changed

docs/api/react/future/configureForms.md

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ import {
253253
type InferCustomFieldMetadata,
254254
} from '@conform-to/react/future';
255255

256-
const result = configureForms({
256+
const forms = configureForms({
257257
extendFieldMetadata(metadata) {
258258
return {
259259
get inputProps() {
@@ -263,39 +263,25 @@ const result = configureForms({
263263
},
264264
});
265265

266-
export const { useForm, FormProvider, config } = result;
266+
// Extract inferred types from config
267+
type BaseErrorShape = InferBaseErrorShape<typeof forms.config>;
268+
type CustomFormMetadata = InferCustomFormMetadata<typeof forms.config>;
269+
type CustomFieldMetadata = InferCustomFieldMetadata<typeof forms.config>;
267270

268271
// Re-export composed types with your custom metadata
269-
// ErrorShape is a constraint - actual errors may be more specific
270-
export type FormMetadata<
271-
ErrorShape extends InferBaseErrorShape<typeof config> = InferBaseErrorShape<
272-
typeof config
273-
>,
274-
> = BaseFormMetadata<
275-
ErrorShape,
276-
InferCustomFormMetadata<typeof config>,
277-
InferCustomFieldMetadata<typeof config>
278-
>;
272+
export type FormMetadata<ErrorShape extends BaseErrorShape = BaseErrorShape> =
273+
BaseFormMetadata<ErrorShape, CustomFormMetadata, CustomFieldMetadata>;
279274

280275
export type FieldMetadata<
281276
FieldShape,
282-
ErrorShape extends InferBaseErrorShape<typeof config> = InferBaseErrorShape<
283-
typeof config
284-
>,
285-
> = BaseFieldMetadata<
286-
FieldShape,
287-
ErrorShape,
288-
InferCustomFieldMetadata<typeof config>
289-
>;
277+
ErrorShape extends BaseErrorShape = BaseErrorShape,
278+
> = BaseFieldMetadata<FieldShape, ErrorShape, CustomFieldMetadata>;
290279

291280
export type Fieldset<
292281
FieldShape,
293-
ErrorShape extends InferBaseErrorShape<typeof config> = InferBaseErrorShape<
294-
typeof config
295-
>,
296-
> = BaseFieldset<
297-
FieldShape,
298-
ErrorShape,
299-
InferCustomFieldMetadata<typeof config>
300-
>;
282+
ErrorShape extends BaseErrorShape = BaseErrorShape,
283+
> = BaseFieldset<FieldShape, ErrorShape, CustomFieldMetadata>;
284+
285+
export const useForm = forms.useForm;
286+
export const FormProvider = forms.FormProvider;
301287
```

examples/chakra-ui/src/forms.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323
ExampleEditable,
2424
} from './form';
2525

26-
const result = configureForms({
26+
const forms = configureForms({
2727
getConstraints,
2828
shouldValidate: 'onBlur',
2929
shouldRevalidate: 'onInput',
@@ -100,36 +100,21 @@ const result = configureForms({
100100
},
101101
});
102102

103-
export type FormMetadata<
104-
ErrorShape extends InferBaseErrorShape<
105-
typeof result.config
106-
> = InferBaseErrorShape<typeof result.config>,
107-
> = BaseFormMetadata<
108-
ErrorShape,
109-
InferCustomFormMetadata<typeof result.config>,
110-
InferCustomFieldMetadata<typeof result.config>
111-
>;
103+
type BaseErrorShape = InferBaseErrorShape<typeof forms.config>;
104+
type CustomFormMetadata = InferCustomFormMetadata<typeof forms.config>;
105+
type CustomFieldMetadata = InferCustomFieldMetadata<typeof forms.config>;
106+
107+
export type FormMetadata<ErrorShape extends BaseErrorShape = BaseErrorShape> =
108+
BaseFormMetadata<ErrorShape, CustomFormMetadata, CustomFieldMetadata>;
112109

113110
export type FieldMetadata<
114111
FieldShape,
115-
ErrorShape extends InferBaseErrorShape<
116-
typeof result.config
117-
> = InferBaseErrorShape<typeof result.config>,
118-
> = BaseFieldMetadata<
119-
FieldShape,
120-
ErrorShape,
121-
InferCustomFieldMetadata<typeof result.config>
122-
>;
112+
ErrorShape extends BaseErrorShape = BaseErrorShape,
113+
> = BaseFieldMetadata<FieldShape, ErrorShape, CustomFieldMetadata>;
123114

124115
export type Fieldset<
125116
FieldShape,
126-
ErrorShape extends InferBaseErrorShape<
127-
typeof result.config
128-
> = InferBaseErrorShape<typeof result.config>,
129-
> = BaseFieldset<
130-
FieldShape,
131-
ErrorShape,
132-
InferCustomFieldMetadata<typeof result.config>
133-
>;
117+
ErrorShape extends BaseErrorShape = BaseErrorShape,
118+
> = BaseFieldset<FieldShape, ErrorShape, CustomFieldMetadata>;
134119

135-
export const useForm = result.useForm;
120+
export const useForm = forms.useForm;

examples/headless-ui/src/forms.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
ExampleRadioGroup,
1616
} from './form';
1717

18-
const result = configureForms({
18+
const forms = configureForms({
1919
getConstraints,
2020
shouldValidate: 'onBlur',
2121
shouldRevalidate: 'onInput',
@@ -49,36 +49,21 @@ const result = configureForms({
4949
},
5050
});
5151

52-
export type FormMetadata<
53-
ErrorShape extends InferBaseErrorShape<
54-
typeof result.config
55-
> = InferBaseErrorShape<typeof result.config>,
56-
> = BaseFormMetadata<
57-
ErrorShape,
58-
InferCustomFormMetadata<typeof result.config>,
59-
InferCustomFieldMetadata<typeof result.config>
60-
>;
52+
type BaseErrorShape = InferBaseErrorShape<typeof forms.config>;
53+
type CustomFormMetadata = InferCustomFormMetadata<typeof forms.config>;
54+
type CustomFieldMetadata = InferCustomFieldMetadata<typeof forms.config>;
55+
56+
export type FormMetadata<ErrorShape extends BaseErrorShape = BaseErrorShape> =
57+
BaseFormMetadata<ErrorShape, CustomFormMetadata, CustomFieldMetadata>;
6158

6259
export type FieldMetadata<
6360
FieldShape,
64-
ErrorShape extends InferBaseErrorShape<
65-
typeof result.config
66-
> = InferBaseErrorShape<typeof result.config>,
67-
> = BaseFieldMetadata<
68-
FieldShape,
69-
ErrorShape,
70-
InferCustomFieldMetadata<typeof result.config>
71-
>;
61+
ErrorShape extends BaseErrorShape = BaseErrorShape,
62+
> = BaseFieldMetadata<FieldShape, ErrorShape, CustomFieldMetadata>;
7263

7364
export type Fieldset<
7465
FieldShape,
75-
ErrorShape extends InferBaseErrorShape<
76-
typeof result.config
77-
> = InferBaseErrorShape<typeof result.config>,
78-
> = BaseFieldset<
79-
FieldShape,
80-
ErrorShape,
81-
InferCustomFieldMetadata<typeof result.config>
82-
>;
66+
ErrorShape extends BaseErrorShape = BaseErrorShape,
67+
> = BaseFieldset<FieldShape, ErrorShape, CustomFieldMetadata>;
8368

84-
export const useForm = result.useForm;
69+
export const useForm = forms.useForm;

examples/material-ui/src/forms.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { getConstraints } from '@conform-to/zod/v3/future';
1212
import type { TextField, Checkbox, RadioGroup, Switch } from '@mui/material';
1313
import type { Autocomplete, Rating, Slider } from './form';
1414

15-
const result = configureForms({
15+
const forms = configureForms({
1616
getConstraints,
1717
shouldValidate: 'onBlur',
1818
shouldRevalidate: 'onInput',
@@ -70,36 +70,21 @@ const result = configureForms({
7070
},
7171
});
7272

73-
export type FormMetadata<
74-
ErrorShape extends InferBaseErrorShape<
75-
typeof result.config
76-
> = InferBaseErrorShape<typeof result.config>,
77-
> = BaseFormMetadata<
78-
ErrorShape,
79-
InferCustomFormMetadata<typeof result.config>,
80-
InferCustomFieldMetadata<typeof result.config>
81-
>;
73+
type BaseErrorShape = InferBaseErrorShape<typeof forms.config>;
74+
type CustomFormMetadata = InferCustomFormMetadata<typeof forms.config>;
75+
type CustomFieldMetadata = InferCustomFieldMetadata<typeof forms.config>;
76+
77+
export type FormMetadata<ErrorShape extends BaseErrorShape = BaseErrorShape> =
78+
BaseFormMetadata<ErrorShape, CustomFormMetadata, CustomFieldMetadata>;
8279

8380
export type FieldMetadata<
8481
FieldShape,
85-
ErrorShape extends InferBaseErrorShape<
86-
typeof result.config
87-
> = InferBaseErrorShape<typeof result.config>,
88-
> = BaseFieldMetadata<
89-
FieldShape,
90-
ErrorShape,
91-
InferCustomFieldMetadata<typeof result.config>
92-
>;
82+
ErrorShape extends BaseErrorShape = BaseErrorShape,
83+
> = BaseFieldMetadata<FieldShape, ErrorShape, CustomFieldMetadata>;
9384

9485
export type Fieldset<
9586
FieldShape,
96-
ErrorShape extends InferBaseErrorShape<
97-
typeof result.config
98-
> = InferBaseErrorShape<typeof result.config>,
99-
> = BaseFieldset<
100-
FieldShape,
101-
ErrorShape,
102-
InferCustomFieldMetadata<typeof result.config>
103-
>;
87+
ErrorShape extends BaseErrorShape = BaseErrorShape,
88+
> = BaseFieldset<FieldShape, ErrorShape, CustomFieldMetadata>;
10489

105-
export const useForm = result.useForm;
90+
export const useForm = forms.useForm;

examples/radix-ui/src/forms.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
ExampleCheckbox,
1818
} from './form';
1919

20-
const result = configureForms({
20+
const forms = configureForms({
2121
getConstraints,
2222
shouldValidate: 'onBlur',
2323
shouldRevalidate: 'onInput',
@@ -65,36 +65,21 @@ const result = configureForms({
6565
},
6666
});
6767

68-
export type FormMetadata<
69-
ErrorShape extends InferBaseErrorShape<
70-
typeof result.config
71-
> = InferBaseErrorShape<typeof result.config>,
72-
> = BaseFormMetadata<
73-
ErrorShape,
74-
InferCustomFormMetadata<typeof result.config>,
75-
InferCustomFieldMetadata<typeof result.config>
76-
>;
68+
type BaseErrorShape = InferBaseErrorShape<typeof forms.config>;
69+
type CustomFormMetadata = InferCustomFormMetadata<typeof forms.config>;
70+
type CustomFieldMetadata = InferCustomFieldMetadata<typeof forms.config>;
71+
72+
export type FormMetadata<ErrorShape extends BaseErrorShape = BaseErrorShape> =
73+
BaseFormMetadata<ErrorShape, CustomFormMetadata, CustomFieldMetadata>;
7774

7875
export type FieldMetadata<
7976
FieldShape,
80-
ErrorShape extends InferBaseErrorShape<
81-
typeof result.config
82-
> = InferBaseErrorShape<typeof result.config>,
83-
> = BaseFieldMetadata<
84-
FieldShape,
85-
ErrorShape,
86-
InferCustomFieldMetadata<typeof result.config>
87-
>;
77+
ErrorShape extends BaseErrorShape = BaseErrorShape,
78+
> = BaseFieldMetadata<FieldShape, ErrorShape, CustomFieldMetadata>;
8879

8980
export type Fieldset<
9081
FieldShape,
91-
ErrorShape extends InferBaseErrorShape<
92-
typeof result.config
93-
> = InferBaseErrorShape<typeof result.config>,
94-
> = BaseFieldset<
95-
FieldShape,
96-
ErrorShape,
97-
InferCustomFieldMetadata<typeof result.config>
98-
>;
82+
ErrorShape extends BaseErrorShape = BaseErrorShape,
83+
> = BaseFieldset<FieldShape, ErrorShape, CustomFieldMetadata>;
9984

100-
export const useForm = result.useForm;
85+
export const useForm = forms.useForm;

examples/react-aria/src/forms.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ declare module '@conform-to/react/future' {
2929
}
3030
}
3131

32-
const result = configureForms({
32+
const forms = configureForms({
3333
getConstraints,
3434
shouldValidate: 'onBlur',
3535
shouldRevalidate: 'onInput',
@@ -143,36 +143,21 @@ const result = configureForms({
143143
},
144144
});
145145

146-
export type FormMetadata<
147-
ErrorShape extends InferBaseErrorShape<
148-
typeof result.config
149-
> = InferBaseErrorShape<typeof result.config>,
150-
> = BaseFormMetadata<
151-
ErrorShape,
152-
InferCustomFormMetadata<typeof result.config>,
153-
InferCustomFieldMetadata<typeof result.config>
154-
>;
146+
type BaseErrorShape = InferBaseErrorShape<typeof forms.config>;
147+
type CustomFormMetadata = InferCustomFormMetadata<typeof forms.config>;
148+
type CustomFieldMetadata = InferCustomFieldMetadata<typeof forms.config>;
149+
150+
export type FormMetadata<ErrorShape extends BaseErrorShape = BaseErrorShape> =
151+
BaseFormMetadata<ErrorShape, CustomFormMetadata, CustomFieldMetadata>;
155152

156153
export type FieldMetadata<
157154
FieldShape,
158-
ErrorShape extends InferBaseErrorShape<
159-
typeof result.config
160-
> = InferBaseErrorShape<typeof result.config>,
161-
> = BaseFieldMetadata<
162-
FieldShape,
163-
ErrorShape,
164-
InferCustomFieldMetadata<typeof result.config>
165-
>;
155+
ErrorShape extends BaseErrorShape = BaseErrorShape,
156+
> = BaseFieldMetadata<FieldShape, ErrorShape, CustomFieldMetadata>;
166157

167158
export type Fieldset<
168159
FieldShape,
169-
ErrorShape extends InferBaseErrorShape<
170-
typeof result.config
171-
> = InferBaseErrorShape<typeof result.config>,
172-
> = BaseFieldset<
173-
FieldShape,
174-
ErrorShape,
175-
InferCustomFieldMetadata<typeof result.config>
176-
>;
160+
ErrorShape extends BaseErrorShape = BaseErrorShape,
161+
> = BaseFieldset<FieldShape, ErrorShape, CustomFieldMetadata>;
177162

178-
export const useForm = result.useForm;
163+
export const useForm = forms.useForm;

0 commit comments

Comments
 (0)