18

It's easy to create a generic pair type ( simplest possible Tuple ) i.e.

type Pair<A, B> = [A, B]

The question is how to create an type which represents and array of such generic pairs.

The only requirement is for element in the array to be a pair. The type of the first element and the type of the second element ought to be polymorphic, any does not make the cut, otherwise this would be satisfactory:

type Pair = [any, any]
type Pairs = Pair[]

1
  • 2
    Wait, why doesn't Array<[any, any]> work? I don't understand what you mean by "polymorphic" here. Commented May 2, 2019 at 0:44

2 Answers 2

26

Pair<T, K> is the same as [T, K] yes, it will do what you want but its syntactically unnecessary.

To create an array of tuples it just would be Array<[TYPE, TYPE]> or [TYPE, TYPE][]

Sign up to request clarification or add additional context in comments.

Comments

15

I feel like I am missing a nuance here.. But believe this is what you are asking for:

type Pair<T,K> = [T,K];
type Pairs<T,K> = Pair<T,K>[];

  const apple: Pair<number,number> = [2,3];
  const orange: Pair<number,number> = [3,4];
  const food: Pairs<number, number> = [apple, orange];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.