I have a problem with typescript Generic
code:
type ValidationError<S> = Record<keyof S, string>;
function validateType<S>(
key: keyof S,
value: string,
type: FieldType | FieldType[],
errors: ValidationError<S>[],
): void {
switch (type) {
case 'email': {
if (!validator.isEmail(value)) {
const error: ValidationError<S> = {
[key]: 'error!',
};
errors.push(error);
}
}
}
}
But I have typescript error "TS2322: Type '{ [x: string]: string; }' is not assignable to type 'Record '." on the:
const error: ValidationError<S> = {
[key]: 'error!',
};
Can someone describe why error occurs and how to fix it?