Open
Description
Summary
With TypeScript, I would like to both use the helper components and have the values passed to my onSubmit handler be typed. In order to do this, I have to call createForm (to get type inference) and pass the resulting context to the Form helper. But when I do this I get a type error saying that my FormState type "is not assignable to type 'FormState<Record<string, any>>".
Example
The following working code produces the error:
<script lang="ts">
import * as yup from 'yup';
import { createForm, Form, Field, ErrorMessage } from 'svelte-forms-lib';
const schema = yup.object().shape({
title: yup.string().required().min(4),
description: yup.string().required().min(10)
});
const context = createForm({
initialValues: {
title: '',
description: ''
},
validationSchema: schema,
onSubmit: (values) => {
alert(JSON.stringify(values));
}
});
</script>
<Form {context}> <!-- TYPE ERROR -->
<div>
<label for="title">Title</label>
<Field type="text" name="title" id="title" />
<ErrorMessage name="title" />
</div>
<div>
<label for="description">Description</label>
<Field type="textarea" name="description" />
<ErrorMessage name="description" />
</div>
<button type="submit">Submit</button>
</Form>
Here is the type error:
Type 'FormState<{ title: string; description: string; }>' is not assignable to type 'FormState<Record<string, any>>'.
Types of property 'updateField' are incompatible.
Type '(field: "title" | "description", value: any) => void' is not assignable to type '(field: string, value: any) => void'.
Types of parameters 'field' and 'field' are incompatible.
Type 'string' is not assignable to type '"title" | "description"'.
Workaround
I am using the following workaround for now, declaring the context to have type 'any':
const context: any = createForm({ ... });
Possible fixes
I suspect that changing Record<string, any>
to {[key: string]: any}
in index.d.ts
might correct the problem.