interface Addressable {
address: string[];
}
Answer from derkoe on Stack OverflowWhy don't arrays have string index signatures?
How to define an array of strings in TypeScript interface? - Stack Overflow
How to Type an array that contains objects and strings
Is Map<string, Array<string|number>> a valid type in TypeScript?
Yes, it works like that. It will create an array that can contain strings and numbers. If you want to have either an array of only strings or an array of only numbers, then you must use Map<string, Array<string> | Array<number>> instead (or shorter syntax Map<string, string[] | number[]>).
What is the syntax for an array of objects in TypeScript?
How do you push an object into a TypeScript array?
How does TypeScript help with error-free arrays of objects?
Videos
interface Addressable {
address: string[];
}
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:
- Using square brackets. This method is similar to how you would declare arrays in JavaScript.
let fruits: string[] = ['Apple', 'Orange', 'Banana'];
- Using a generic array type, Array.
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];
Source
Hello everyone.
I'm having a hard time getting the linter to accept the typing of an array of mostly objects that can contain a string.
exampleArray: [{id: 'abc', name: 'test2'}, {id: 'abc', name: 'test2'}, 'ALL'];I have typed the array accordingly:
export interface ITest {
id?: string;
name?: string;
}
export type TestOption = ITest | string;I can't get the tests to pass. Examples of the test outputs are found below:
#``1:
error TS2339: Property 'displayValue' does not exist on type 'string | ITest'.
Property 'name' does not exist on type 'string'.
const labelId = `${multiple ? 'checkbox-' : ''}list-label-${option?.name || ''}`;#2:
src/components/FilterSelect/index.tsx:274:142 - error TS2339: Property 'displayValue' does not exist on type 'string | ITest'.
Property 'name' does not exist on type 'string'.
274 <ListItemText classes={{ primary: classes.listItemText, root: classes.listItemTextRoot }} id={labelId} primary={option.name} />If anyone has any advice or documentation that will help please send it my way! 🙏