我已定义的接口IDictionary
作为类型T的参数如下:参数不能分配给
interface IDictionary<T = any> {
[key: string]: T;
}
再后来我通过这种类型Ť到db.update()
函数被定义为:
public update<T = IDictionary>(path: string, value: T) {
return this.ref(path).update(value);
}
当我尝试编译此文件时,出现如下错误:
src/shared/Model.ts (64,7): Argument of type '{ lastUpdated: string; }' is not assignable to parameter of type 'T'. (2345)
考虑到IDictionary<any>
类型是默认的“T”,我不明白为什么这不会被认为是有效的。有什么我做错了吗?
完整的例子:
interface IDictionary<T = any> {
[key: string]: T;
}
function dbupdate<T = IDictionary>(path: string, value: T) {
return this.ref(path).update(value);
}
abstract class Model<T = IDictionary> {
public update(updateWith: IDictionary) {
const reference = 'bar';
return dbupdate<T>(
reference,
{
...updateWith,
...{ lastUpdated: 'baz' }
}
);
}
}
问题不在于您发布的代码中,而是您如何使用它。附加的图像不显示所有相关的代码,只显示其结尾。这是什么'T'? –
在上面的图片中,您可以在悬停对话框中看到类型分辨率...默认为“IDictionary' –
ken
请使用其他相关代码更新您的问题。如果有其他人可以用来重现问题的东西,那就太好了。 –