I have an object like this:
{
"address": ["line1", "line2", "line3"]
}
How to define address in an interface? the number of elements in the array is not fixed.
I have an object like this:
{
"address": ["line1", "line2", "line3"]
}
How to define address in an interface? the number of elements in the array is not fixed.
interface Addressable {
address: string[];
}
Customer and you need to have another parent interface which should have List then it would be like Customer[]An array is a special type of data type which can store multiple values of different data types sequentially using a special syntax.
TypeScript supports arrays, similar to JavaScript. There are two ways to declare an array:
let fruits: string[] = ['Apple', 'Orange', 'Banana'];
let fruits: Array<string> = ['Apple', 'Orange', 'Banana'];
Both methods produce the same output.
Of course, you can always initialize an array like shown below, but you will not get the advantage of TypeScript's type system.
let arr = [1, 3, 'Apple', 'Orange', 'Banana', true, false];
I think the best way to do it is something like:
type yourtype = { [key: string]: string[] }
Here is how the cool kids do it nowadays.
export type stringArray = [string][number][];
Explanation:
[string] creates a tuple of string
[number] cast the tuple into a string
[] declares it as an array
For non believers, here is the proof:
String[] ? (Aside from it not being as cool)string[] other than being less readable. but at least you learn about type casting in Typescript.