dorondo
dorondo

Reputation: 73

Allow any subset of specified array in Typescript

Can I specify an array as a type in Typescript and then any subset of this array is allowed to be passed?

E.g.

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

Upvotes: 0

Views: 35

Answers (1)

jasper
jasper

Reputation: 945

This should work

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

Upvotes: 2

Related Questions