1

I have a dataset

"series": [{
    "name": "Events",
    "data": [
        [0,0],
        [0,1],
        [2,2],
        ...
    ]
}]

What's the proper Typescript interface for this? This is not quite right:

export interface MyInterface {
    series: {
        name: string;
        data: number[]
    }
}
1
  • 1
    have you tried data: number[][] ? Commented Dec 23, 2016 at 23:24

2 Answers 2

3

If data is always two number elements, you should use a tuple type: [number, number].

interface MyType {
    series: {
        name: string;
        data: [number, number][]
    }[]
};

If not, you should use a 2 dimensional array: number[][].

interface MyType {
    series: {
        name: string;
        data: number[][]
    }[]
};
Sign up to request clarification or add additional context in comments.

Comments

0

I prefer to keep my typings as a single level/hierarchy, if it requires another level to describe the object, I like to create its own interface/type for that object.

Example:

export interface SeriesEvent {
    [0]: number,
    [1]: number
}

export interface Series {
    data: SeriesEvent[]
    name: "Events";
}

export interface MyInterface {
    series: Series
}

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.