下面是 Type 的枚举声明,共有 6 个字段

enum Type {
  primary = "primary",
  success = "success",
  warning = "warning",
  warn = "warn", // warning alias
  danger = "danger",
  info = "info",
}

TypeScript 中声明类型的关键字有两个,interface 和 type,在声明 key 不确定类型的字段时稍有不同。

  1. 使用 type 进行声明:
type ColorConfig = {
  [key in Type]: Colors;
};
  1. 使用 interface 却只能像下面这样:
interface ColorConfig {
  [key: string]: Colors;
}

因为 interface 的索引只能是基础类型,类型别名也不可以。而 type 的索引可以是复合类型。

Logo

前往低代码交流专区

更多推荐