打字稿在这种情况下如何重用打字稿?

时间:2019-08-23 04:48:24

标签: typescript

interface Test {
  name: "abcd" | "efgh" | "ijkl";
}

interface Human {
  name: "abcd" | "efgh" | "ijkl"
}

我想与Human.name一起重用Test.name的类型,但是如何?有什么想法吗?

2 个答案:

答案 0 :(得分:2)

将名称提取为联合类型

type NameTypes = "abcd" | "efgh" | "ijkl";

interface Test {
  name: NameTypes;
}

interface Human {
  name: NameTypes;
}

答案 1 :(得分:1)

这也应该起作用

interface Test {
  name: "abcd" | "efgh" | "ijkl";
}

interface Human extends Test {

}