我对打字稿通用有疑问
代码:
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我有打字稿错误“TS2322:类型 '{ [x: string]: string; }' 不可分配给类型 'Record '。” 关于:
const error: ValidationError<S> = {
[key]: 'error!',
};
Run Code Online (Sandbox Code Playgroud)
有人可以描述为什么会发生错误以及如何修复它吗?