2
type MyArray<T> = [data: T[] , count: number ]; // doesn't work

On the client side, I want to use MyArray type to map data from an API and to be able to call its 'data' or the 'count' property. The API delivers the data in an array, at index 0 the array has the data and at index 1 it has a number.

1
  • So you have two types, one is type MyArray<T> = [T[], number], and the other is type MyData<T> = {data: T[], count: number}. I don't think it makes sense to express that as a single type... you really want a function that takes a MyArray<T> and returns a MyData<T>. Commented Feb 26, 2019 at 14:18

1 Answer 1

4

You are looking for a tuple type:

type MyArray<T> = [T[], number];

Tuple members can't have names, they are only positional and are resented at runtime using arrays

Usage example:

type MyArray<T> = [T[], number];
let data : MyArray<string> = [["data1", "data2"], 2]
let arr = data[0] // string[]
let count  = data[1] // number
Sign up to request clarification or add additional context in comments.

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.