TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

接口-可索引的类型

Open luckyriko opened this issue 3 years ago • 1 comments

image 我是TS初学者 这一块看不懂 有可以正常运行的示例吗

luckyriko avatar Mar 30 '23 19:03 luckyriko

@luckyriko

interface NumberOrStringDict {
  [index: string]: number | string;
  length: number; // ok, length is a number
  name: string; // ok, name is a string
}

这个是一种正确的用法。

如果把 [index: string]: number | string; 写成 [index: string]: number;[index: string]: string; 都不可以。

zhongsp avatar May 11 '23 06:05 zhongsp