I want to have an array of an object as follows.
However typescript throws up an error Property 0 is missing in type []
let organisations: [{name: string, collapsed: boolean}] = [];
What you are defining is a tuple type (an array with a fixed number of elements and heterogeneous types). Since tuples have a fixed number of elements the compiler checks the number of elements on assignment.
To define an array the []
must come after the element type
let organisations: {name: string, collapsed: boolean}[] = [];
Or equivalently we can use Array<T>
let organisations: Array<{name: string, collapsed: boolean}> = [];
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments