我可以在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
答案 0 :(得分:3)
这应该有效
type Action = 'view' | 'share' | 'delete'
type Actions = Action[]