亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
TypeScript 教程 / 數(shù)組

數(shù)組

Readonly

readonly 關鍵字可以防止數(shù)組被更改。

實例

const names: readonly string[] = ["Dylan"];
names.push("Jack"); // 錯誤:類型 'readonly string[]' 上不存在屬性 'push'。
// 嘗試刪除 readonly 修飾符,看看是否有效?

類型推斷

如果數(shù)組有值,TypeScript 可以推斷數(shù)組的類型。

實例

const numbers = [1, 2, 3]; // inferred to type number[]
numbers.push(4); // 無錯誤
// 注釋掉下面的行以查看成功的分配
numbers.push("2"); // 錯誤:類型為 'string' 的參數(shù)不能分配給類型為 'number' 的參數(shù)。  
let head: number = numbers[0]; // 無錯誤