有沒有辦法強制打字稿更嚴格的類型檢查,直到每個別名檢查?Typescript嚴格別名檢查
我想實現的是定義類型,如:
type kilograms = number;
type kilometers = number;
type kilogramsPerKilometer = number;
而且可以肯定的是我不把錯誤類型值到一個變量如:
let x: kilograms = 123;
let y: kilometers = 256;
let z: kilogramsPerKilometer = x/y; // Will popup an error here saying that types are incompatible
在這種情況下,需要顯式類型轉換:
let x: kilograms = 123;
let y: kilometers = 256;
let z: kilogramsPerKilometer = <number>x/<number>y; // Will downcast `kilograms` and `kilometers` types to `number` and then up-cast types to `kilogramsPerKilometer`
這種方法如何執行嚴格的別名檢查? – Lu4