I have devised a basic Form
class using TypeScript:
class Form<FormData> {
protected data: FormData;
constructor(data: FormData) {
this.data = data;
}
}
To ensure the form receives specific data upon instantiation, I included a type parameter. For instance:
interface UpdateUserForm {
name: string;
email: string;
}
const updateUserForm = new Form<UpdateUserForm>;
Yet, when attempting to utilize my Form
class as an argument in other classes, I encounter an issue as it requires knowing the type. Consider a method like this:
class Http {
post(url: string, form: Form) {
return axios(url, form.data);
}
}
This triggers the following error:
Generic type 'Form' requires 1 type argument(s).
Sadly, I lack knowledge of the FormData
type in this generic method.
How can I correctly provide type information for the form
parameter?