2

TypeScript newbie here. I did some quick searching but was unable to find an answer. The question is this: what is the proper definition for an array of tuples, where the first tuple element is a string, and the next is an array of numbers? I've tried this:

type labeledValuesType = [string, number[]]

 constructor(headings: string[], rows: Array<labeledValuesType>) {...

But in VS Code, when I hover over the second parameter (rows) passed to the constructor call, I see:

const rows: (string | number[])[][]

which looks like a two-dimensional array of the union of string and number[].

1
  • Could you provide a true minimal reproducible example which demonstrates the issue? Reproducing the issue in a standalone environment like the TypeScript Playground is a good way to know that we're all on the same page. As it stands, the type of rows in your code shows as (parameter) rows: [string, number[]][] in my IDE. Perhaps you are looking at the type of some variable you haven't included in the question? Commented Aug 21, 2019 at 18:06

1 Answer 1

2

An array of tuple, which have

  • a string as first element,
  • an array of number as second element

If I'm not wrong on what you're searching:

type MyTuple = [string, number[]];

const myArray: MyTuple[] = [
    ['str', [1, 2, 3]]
];

So your code seems OK, your issue concerns more VS code.

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

1 Comment

I think the problem in the original code is that Array<labeledValuesType> takes a type (labeledValuesType) that is already an array, then wraps it in another Array<>?

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.