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
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!
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.
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.
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.
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[] } = {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With