📜  ts 对象可以强类型吗? - 打字稿代码示例

📅  最后修改于: 2022-03-11 14:48:36.098000             🧑  作者: Mango

代码示例1
// This defines an interface that only allows values to be numbers
interface INumbersOnly {
  [key: string]: number;
}

// when using it, it will check that all properties are numbers
var x: INumbersOnly = {
  num: 1, // works fine
  str: 'x' // will give a type error
};