【发布时间】:2019-09-04 02:23:07
【问题描述】:
给定这个函数:
export const combineValidators = <Input extends { [P in keyof Input]: (val: string) => Err }, Err>(
validators: Input
) => (values: { [P in keyof Input]?: unknown }): { [P in keyof Input]: Err } => {
// Ignore implementation.
return {} as { [P in keyof Input]: Err };
};
还有这个用法:
const validator = combineValidators({
name: (val) => val ? undefined : 'error',
email: (val) => val ? undefined : 'error'
});
const errors = validator({
name: 'Lewis',
email: 'lewis@mercedes.com'
});
我希望 TypeScript 能够将返回类型推断为:
// Expected: `errors` to be inferred as:
interface Ret {
name: string | undefined;
email: string | undefined;
}
但是推断为:
// Actual: `errors` inferred as:
interface Ret {
name: {};
email: {};
}
我创建了一个 live example in the TypeScript playground 来演示这个问题。
有人可以帮忙吗?
【问题讨论】:
标签: javascript typescript