2

I want to create a type that only accepts a specific combination of array values regardless of their order.

For example:

const acceptedCombo = ['a', 'b'];

function foo(param: MyType) {}

// Possible combos:
['a', 'b'] // OK
['b', 'a'] // OK
['a', 'b', 'c'] // TypeError - "c" is extra
['a'] // TypeError - "b" is missing

How can I define MyType?

cdruc
  • 574
  • 2
  • 10

1 Answers1

0

You can do it by this way:

type MyType = [string, string];
Usitha Indeewara
  • 870
  • 3
  • 10
  • 21
  • 1
    this doesn't restrict the string to being 'a' and 'b' it could literally be anything. captain-yossarian from Ukraine answer in the comments should be sufficient. – Derek Lawrence Nov 30 '22 at 16:38