Skip to content

Commit 0a3d9b8

Browse files
sedxerikras
authored andcommitted
Pass FormValues type to Decorator (final-form#661)
* Pass FormValues type to Decorator * Update after automatic review
1 parent 74e1cc9 commit 0a3d9b8

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

typescript/ReactFinalForm.test.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* tslint:disable: no-shadowed-variable */
2-
import { Mutator } from 'final-form';
2+
/* eslint-disable @typescript-eslint/no-unused-vars */
3+
4+
import { Decorator, Mutator } from 'final-form';
35
import * as React from 'react';
46
import { Field, Form } from 'react-final-form';
57

@@ -131,15 +133,20 @@ function mutated() {
131133
);
132134
}
133135

134-
const typedOnSubmit = (values: { firstName: string }) => {
136+
interface UserForm {
137+
firstName: string;
138+
lastName: string;
139+
}
140+
141+
const typedOnSubmit = (values: UserForm) => {
135142
// tslint:disable-next-line no-console
136143
console.log(values);
137144
};
138145

139146
// with typed form data and field
140147
function withTypedFormData() {
141148
return (
142-
<Form<{ firstName: string }> onSubmit={typedOnSubmit}>
149+
<Form<UserForm> onSubmit={typedOnSubmit}>
143150
{({ handleSubmit }) => (
144151
<form onSubmit={handleSubmit}>
145152
<div>
@@ -157,3 +164,25 @@ function withTypedFormData() {
157164
</Form>
158165
);
159166
}
167+
168+
const decorator: Decorator<UserForm> = form => {
169+
return form.subscribe(({ values: { firstName } }) => firstName, {
170+
values: true
171+
});
172+
};
173+
174+
// with typed decorator
175+
function withTypedDecorator() {
176+
return <Form<UserForm> decorators={[decorator]} onSubmit={typedOnSubmit} />;
177+
}
178+
179+
// with wrong typed decorator
180+
function withWrongTypedDecorator() {
181+
return (
182+
<Form<Omit<UserForm, 'firstName'>>
183+
// $ExpectError
184+
decorators={[decorator]}
185+
onSubmit={noop}
186+
/>
187+
);
188+
}

typescript/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface FormProps<FormValues = AnyObject>
6767
extends Config<FormValues>,
6868
RenderableProps<FormRenderProps<FormValues>> {
6969
subscription?: FormSubscription;
70-
decorators?: Decorator[];
70+
decorators?: Array<Decorator<FormValues>>;
7171
form?: FormApi<FormValues>;
7272
initialValuesEqual?: (a?: AnyObject, b?: AnyObject) => boolean;
7373
}

0 commit comments

Comments
 (0)