How to create an empty typed array in TypeScript?
Hello everyone! I just started learning TypeScript after JavaScript and ran into a problem when creating empty arrays. In JavaScript I often did this:\n\n```javascript\nlet items = [];\n```\n\nAnd then just added elements using push. But in TypeScript I get an error when trying to do the same:\n\n```typescript\nlet items = [];\nitems.push('hello'); // Error: Argument of type 'string' is not assignable to parameter of type 'never'\n```\n\nI understand that TypeScript infers the type `never[]` because the array is empty, and the compiler doesn't know what elements will be there. But how do I correctly declare an empty array that will later contain, for example, strings or objects of a specific type?\n\nI tried several options:\n\n```typescript\nlet items: string[] = []; // Works, but for st
No answers yet.
Log in to leave an answer