Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is in Typescript possible to define 'object of arrays of type string? (right position of type checking?)

Tags:

typescript

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

like image 986
dragonmnl Avatar asked Apr 26 '16 09:04

dragonmnl


People also ask

How do you define an object array in TypeScript?

To declare an array of objects in TypeScript, set the type of the variable to {}[] , e.g. const arr: { name: string; age: number }[] = [] . Once the type is set, the array can only contain objects that conform to the specified type, otherwise the type checker throws an error. Copied!

How do you define object of objects type in TypeScript?

In TypeScript, object is the type of all non-primitive values (primitive values are undefined , null , booleans, numbers, bigints, strings). With this type, we can't access any properties of a value.

How do you declare an array of types in TypeScript?

In TypeScript, an array is an ordered list of values. An array can store a mixed type of values. To declare an array of a specific type, you use the let arr: type[] syntax.

Is string an object in TypeScript?

In TypeScript, the string is an object which represents the sequence of character values. It is a primitive data type which is used to store text data. The string values are surrounded by single quotation mark or double quotation mark. An array of characters works the same as a string.


1 Answers

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[] } = {};
like image 148
Nitzan Tomer Avatar answered Sep 28 '22 10:09

Nitzan Tomer