0

There is already a similar question, only difference is that I want to declare the typings in a d.ts file instead of declaring every time.

I have been writing typings like

interface SomeType {
 key1: string[];
 key2: number;
}

which are Objects with named properties type.


Now I need an Array with two sub-Arrays type,

i.e (something like):

 TheType : [string[], string[]]

So that I can write

let myVar: TheType;

instead of

let myVar: [string[], string[]];

I've Tried to play with namespace, module, declare var with no luck, I can explain the problems with them if required, but I just feel that either I'm missing something very obvious or it's just not possible ?!

Note: workarounds !needed, thanks!

1 Answer 1

2

I would mix an interface extending Array with a generic to create a NestedArray type, which you can use with any type. Having a nested array type will make your inline types more readable:

interface NestedArray<T> extends Array<Array<T>> {}

var x: NestedArray<string> = [['a', 'b'], ['c', 'd'], ['e', 'f']];

var y = x[0]; // y is Array<string>
var z = x[0][1]; // z is string

Use with other types:

var x: NestadArray<Customer> = [];
Sign up to request clarification or add additional context in comments.

1 Comment

NestedArray seems legit, nice

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.