![](/img/trans.png)
[英]Types of parameters '__0' and 'value' are incompatible - Typescript
[英]Types incompatible for function parameters
const a: Test = {
as: (d: Dust) => {
return d.b;
}
};
interface Test {
as: (d: Tust) => void;
}
interface Tust {
a: string;
}
interface Dust extends Tust {
b: boolean;
}
忽略接口名稱,這會產生警告:
類型 '(d: Dust) => Dust' 不可分配給類型 '(d: Tust) => void'。 參數“d”和“d”的類型不兼容。
但它有效,正確的方法是如何防止錯誤。
提前致謝。
來自 TypeScript 的有效錯誤。 原因如下:
const a: Test = {
as: (d: Dust) => { // Error: you cannot accept Dust for a Tust
return d.b;
}
};
interface Test {
as: (d: Tust) => void;
}
interface Tust {
a: string;
}
interface Dust extends Tust {
b: boolean;
}
// Why: Because it can be called with Tust
a.as({
a: 'okay' // Allowed
})
如何正確的方法來防止錯誤。
接受一個Tust
而不是Dust
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.