【发布时间】:2018-12-18 00:00:29
【问题描述】:
如果我尝试将对象字面量与具有类型约束的泛型类型一起使用,则会出现类型错误,我正在努力找出原因:
type WithKey = {
readonly akey: string;
}
function listOfThings<T extends WithKey>(one: T) {
// This is ok
const result2: Array<T> = [one];
//This errors with Type '{ akey: string; }' is not assignable to type 'T'.
const result: Array<T> = [{ akey: 'foo' }];
return result;
}
【问题讨论】:
-
您希望调用
const hmm = listOfThings({akey: "foo", bkey: 123});会产生什么结果? -
最近been proposed 表示,该错误虽然准确,但应该减少混淆。
标签: typescript