typescript - 如何使用可选的唯一字符串定义数组 typescript 接口(interf

我想定义一个带有可选字符串值的接口(interface)。像这样的东西:

interface IEntity {
    values: ['RemainingUnits', 'ActualUnits', 'PlannedUnits']
}

但是使用这个界面我遇到了问题:

const entity0: IEntity = { values: ['PlannedUnits'] }; // => Error
const entity1: IEntity = { values: ['RemainingUnits', 'ActualUnits'] }; // => Error
const entity2: IEntity = { values: ['PlannedUnits', 'RemainingUnits', 'ActualUnits'] }; // => Error

那么有没有办法编写正确的接口(interface)来避免上面的错误呢?

并且完全没有重复的字符串并且不为空

最佳答案

也许:

type Units = 'RemainingUnits' | 'ActualUnits' | 'PlannedUnits';

interface IEntity {
  values?: Units[];
}

关于typescript - 如何使用可选的唯一字符串定义数组 typescript 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61874451/

相关文章:

python - 生成最多 2 个重复的 3 个数字的列表

python - 在对数刻度上生成均匀间隔的值(如 `np.linspace()` ,但在对数刻度上

javascript - 谷歌邮箱 : Button/Link which trigger a sc

react-native - React 导航 5(导航到另一个堆栈屏幕)

forms - 如何判断表单字段在 vue.js 中是否无效

amazon-web-services - CognitoIdentityCredentials 不

python - matplotlib 为单个散点图添加具有多个条目的图例

swift - 如何不 dispatch 如此频繁?

amazon-web-services - 有没有办法使用 aws cloudformation 更

python - 我怎样才能看到 Pandas 的几个最大值?