允许Typescript中指定数组的任何子集

时间:2019-11-20 11:41:42

标签: typescript

我可以在Typescript中将数组指定为类型,然后允许传递该数组的任何子集吗?

例如

type Actions = ['view', 'share', 'delete']

const concatActions = (actions: Actions) => actions.join();

concatActions(['view', 'delete']) // Works
concatActions(['share']) // Works
concatActions(['view', 'some_other_action']) // Throws type error

1 个答案:

答案 0 :(得分:3)

这应该有效

type Action = 'view' | 'share' | 'delete'
type Actions = Action[]