How to Declare an Empty Array in TypeScript? Last Updated : 03 Oct, 2024 Comments Improve Suggest changes Like Article Like Report TypeScript provides a robust type system that allows you to define and manage data structures more effectively than in plain JavaScript. One common task is declaring empty arrays with specific types, ensuring that your array holds only values of the intended type. These are the following ways to declare an empty array in TypeScript:Table of ContentDeclaring an Empty Array of a Specific TypeDeclaring an Array That Can Hold Multiple TypesDeclaring an Empty Array of a Specific TypeIn TypeScript, you can declare an empty array that will only accept elements of a particular type. The most common approach is using the square bracket syntax to specify the type of the array. This ensures that only numbers can be added to the numbers array. Trying to add a value of a different type (e.g., a string) will result in a compilation error.Example: In this example, we will declare an empty array of numbers that can only hold numbers. JavaScript let numbers: number[] = []; numbers.push(1); numbers.push(2); console.log(numbers); // Output: [1, 2] Output:[1, 2]Declaring an Array That Can Hold Multiple TypesSometimes you may need an array that can store values of different types. TypeScript allows you to use union types to declare such arrays. This approach is useful when you need a flexible array that can store different types of data, but you still want the type safety provided by TypeScript.Example: In this example, the array can hold both numbers and strings. JavaScript let mixedArray: (number | string)[] = []; mixedArray.push(1); mixedArray.push("Hello"); console.log(mixedArray); // Output: [1, "Hello"] Output:[1, "Hello"] Comment More infoAdvertise with us Next Article How to Declare an Empty Array in TypeScript? S sharmasahtqzx Follow Improve Article Tags : Web Technologies TypeScript Similar Reads How to Clone an Array in TypeScript ? A cloned array is an array that contains all the items or elements contained by an already existing array in the same order. A cloned array does not affect the original array. If there is any change made in the cloned array it will not change that item in the original array. We can use the following 5 min read How to Declare a Fixed Length Array in TypeScript ? To declare a Fixed-length Array in TypeScript you can use a Tuple. Tuple types allow you to specify the types for each element in the array and, importantly, define a fixed number of elements in a specific order. In this article, we are going to learn how to declare a fixed-length array in TypeScrip 3 min read How to Declare an Array of Strings in TypeScript ? Arrays are fundamental data structures in TypeScript, enabling developers to manage collections of elements efficiently. Below are the approaches to declare an Array of strings in TypeScript:Table of ContentSquare Brackets NotationArray ConstructorSquare Brackets NotationUsing square brackets notati 1 min read Check if an Array is Empty or not in TypeScript In TypeScript, while performing any operation on the array, it is quite important that there should be elements or data in the array, else no operation can be done. We can check whether the array contains the elements or not by using the below-listed methods: Table of Content Using length PropertyUs 2 min read How to Avoid Inferring Empty Array in TypeScript ? In TypeScript, the empty arrays are inferred as never[] by default, denoting an array incapable of containing any elements. This occurs when TypeScript fails to deduce the type of array elements from the context, notably in scenarios involving spread operators, the Array constructor, or methods like 2 min read Like