声明值类型为打字稿中的数组值之一

时间:2019-06-04 09:26:31

标签: arrays typescript

我有一个键和值都为字符串的对象。
const obj: {[key: string]: string} = {first: 'a', second: 'b'}

我想声明对象值必须是数组值之一:
示例:

const arr: string = ['a', 'b', 'c']
const b: {[key: string]: string/*valueOf(arr)*/} = {
  first: 'a', // to be legal
  second: 'f', // to throw an error
}

搜索了类似的case,但在我的情况下,arr是数组而不是类型。

我如何宣布这一权利?

0 个答案:

没有答案