【发布时间】:2020-11-15 07:30:22
【问题描述】:
我对 typescript Generic 有疑问
代码:
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);
}
}
}
}
但我有打字稿错误“TS2322: Type '{ [x: string]: string; }' is notassignable to type 'Record'。”在:
const error: ValidationError<S> = {
[key]: 'error!',
};
有人可以描述为什么会发生错误以及如何解决它吗?
【问题讨论】:
标签: typescript typescript-generics