
In this short tutorial, Learn how to create an empty typed array in typescript.
It includes multiple ways to create a string-typed empty array.
Let us declare Employee Interface in the ts file.
interface Employee {
name: string;
id: number;
salary: number;
}
Create an empty typed array interface or class example
Employee object holds different types of data. The empty typed array is created in many ways.
Create an empty typed array interface Using generics type declaration
First, the array variable can be declared with a generic array of Employees by assigning an empty array.
let employees: Array<Employee> = [];
Thus, create a variable store’s empty typed array.
Create an empty typed array interface type assertion
`type assertion is like assigning an object of one type to a variable. This will have no performance impact at runtime. however, It can be used to avoid errors at compile time.
Declared with two syntaxes.
the first as syntax
and the second is angle-bracket syntax
.
The following is an example for creating an empty typed array with as syntax
.
let empArray1 = [] as Employee[];
The above syntax can also be written with angle-bracket syntax as follows.
let empArray = <Employee[]>[];
Both are equal in terms of usage and performance.
Create an empty typed array interface Array constructor
Finally, array constructors are generic usage and used by every developer. It uses a new operator to create an empty array.
let empArray = new Array<Employee>();
let empArray: Array<Employee> = new Array<Employee>();
It looks good in readability, however, performance impacts creating a reference in memory.
How to create an empty typed string array with examples
There are many ways typed string arrays are created with the following syntaxes.
let emptyStringArray: string[] = [];
let emptyStringArray1 = new Array<string>();
let emptyStringArray2: Array<string> = new Array<string>();
Steps:
- The first line is with inline empty array initialization
- The second line is a Create using a new keyword
- The third line is an Array with a new object
How to Create and initialize typed empty objects in an array
In this example, Let’s create a non-empty array with object data that is empty or default values.
let us declare an interface for the User
interface User {
name: string;
id: number;
password: string;
}
Following is an example of an array initialized with three empty user objects.
Partial in typescript
allows you to construct an object with optional fields
Partial<User>
return User with optional id, name, and password
users: Partial<User>[] = [{}, {}, {}];
And another option is to manually set values for each object with the default values of an object.
users1: User[] = [{ id: null, name: null, password: null }{ id: null, name: null, password: null }]
You can check another post on Fix for Object is possibly null
Conclusion
To sum up, you learned different ways to create a typed empty array. And also how to create an array with empty objects.
Please subscribe for new posts.