New to Typescript and got stuck on a basic problem. I would like to push objects to an array like so:
type SomeArray = [
{
id: string;
}
];
const someArray: SomeArray = [];
The first problem is that it does not allow me to initialize with an empty array. Do I really need a union for this? i.e. SomeArray = [...] | []?
Next problem is that I can't push items to it:
type SomeArray = [
{
id: string;
}
] | [];
const someArray: SomeArray = [];
someArray.push({ id: 'a' })
Type 'string' is not assignable to type 'never'.(2322)
What is the correct way to define an array that accepts a typed object?