[英]Stricter object literal assignment and union types
從TypeScript 1.6開始,有更嚴格的對象文字分配
但正如我所見,它不適用於聯合類型。
module Test {
interface Intf { name: string; }
function doWorkWithIntf(param: Intf) {}
function doWorkWithUnionType(param: Intf | string) {}
function doWork2() {
//doWorkWithIntf({name: "", xx: 10}) // does not compile TS2345
doWorkWithUnionType({name: "", xx: 10}) // do compile
}
}
是我的錯誤還是編譯器錯誤? 我使用TS 1.7.5
編譯器的最新版本肯定可以解決此問題,但1.8.0或更低版本沒有。
該功能仍然存在,例如:
var example: Intf = { name: '', xx: 10 };
將不會在1.8.0中編譯。 但是,您的版本需要更多的推斷,而且較舊的編譯器似乎不會解壓縮並列類型以檢查該值。
您可以在TypeScript操場上看到正在處理的簡單案例和復雜案例。
module Test {
interface Intf { name: string; }
function doWorkWithIntf(param: Intf) {}
function doWorkWithUnionType(param: Intf | string) {}
function doWork2() {
//doWorkWithIntf({name: "", xx: 10}) // does not compile TS2345
doWorkWithUnionType({name: "", xx: 10}) // do compile
}
var inf: Intf = { name: '', xx: 10 };
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.