我已定義的接口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
請使用其他相關代碼更新您的問題。如果有其他人可以用來重現問題的東西,那就太好了。 –