Please, tell me, can I do this with ts? I have next source code:
interface FormStore<T> {
type: T;
}
interface Form<T> extends FormStore<T> {
email: T;
phone: T;
password: T;
}
interface FormState<R> {
form: Form<string>;
validate: Form<boolean>;
}
I want to reuse type FormState<R>
and create new with argument. It something looks like replace Form
on R
:
// This example doesn't work, just only for example
interface FormState<R> {
form: R<string>;
validate: R<boolean>;
}
// another file
FormState<CustomForm>
CodePudding user response:
From your original declaration, this is the best I can do for now. If you don't make set a default type parameter for FormStore
& Form
, you will have to explicitly use type FormStateF = FormState<Form<unknown>>;
, which is awkward.
interface FormStore<T = unknown> {
type: T;
}
interface Form<T = unknown> extends FormStore<T> {
email: T;
phone: T;
password: T;
}
interface FormState<R> {
form: { [x in keyof R]: string };
validate: { [x in keyof R]: boolean };
}
type FormStateF = FormState<Form>;