9

I know in typescript an object can be typed either by ClassName (ES6 class) or by 'any'.

I also know you can define an array of string (string[]) and even an array of arrays of string (string[][]).

I need to express the type of an object whose properties are only arrays of type string.

e.g.

export var MY_VAR: any = {
    <string[]> p1: [...]
}

I tried with something like any following object_name: but not luck.

I also tried any following object_name and before each object's array.

In either case I have syntax errors (see example above)

EDIT apparently

export var MY_VAR: any = {
    <string[]> p1: [...]
}

works instead. however I don't understand what the difference is

1 Answer 1

30

It's not very clear what you're asking for, but if I managed to get you right then:

interface MyType {
    [key: string]: string[];
}

Or inline:

let myArrays: { [key: string]: string[] } = {};
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. can you just clarify the difference between <string[]> p1 and p1: string[] (see my 1st and 2nd example)?
Basically <string[]> p1 means casting p1 to a string array (string[] which is like Array<string>). But in your example you cast the key to be a string array instead of the value. But even if you'd cast the value, it's just one specific value, it means nothing about the object itself (which you defined as any)

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.