I need to define two types:
- Type for object which holds some data with values of defined types
- Type for object which holds validators for the values contained in object with the first type
My realization:
type Data = Record<string, string|number|Array<string>>
type Validators<T extends Data> = {
[P in keyof T]?: (value: T[P]) => string
}
interface MyData extends Data {
first: string,
second: number,
third: Array<string>,
}
const validators: Validators<MyData> = {
first: (value: string) => "",
};
With this realization i get the error for the validators object:
TS2322: Type '{ first: (value: string) => string; }' is not assignable to type 'Validators'.
Property 'first' is incompatible with index signature.
Type '(value: string) => string' is not assignable to type '(value: string | number | string[]) => string'.
Types of parameters 'value' and 'value' are incompatible.
Type 'string | number | string[]' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
Is it possible to define such types in typescript?