{

Typescript - Beginner Guide to type keyword with example


typescript type keyword tutorials example In this short tutorial, learn about the type keyword in typescript with examples.

Type is a keyword in typescript, used as an alias for existing or custom types.

type keyword examples

You can check other post on Fix for Object is possibly null Following are example

How to declare a type nullable

Let us declare Employee Interface in the ts file

interface Employee {
  name: string;
  id: number;
  salary: number;
}

Create an empty typed array of an interface or class

Employee object holds different types of data.

Using generics type declaration

First, the array variable can be declared with a generic array of Employeeby assigning an empty array

let employees: Array<Employee> = [];

Thus, create a variable store’s empty typed array

type assertion

`type assertion is like assigning an object of one type to a variable. It will have no performance impact at runtime. however, It is used to avoid errors at compile time.

two syntaxes are used. The first is as syntaxand the second isangle-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.

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 array’s can be created with the following syntaxes

let emptyStringArray: string[] = [];
let emptyStringArray1 = new Array<string>(); 
let emptyStringArray2: Array<string> = new Array<string>();

Create and initialize typed empty objects in an array

In this example, 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 with 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 manual 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 }]

Conclusion

In Conclusion, you learned different ways to create a typed empty array, and also how to create an array with empty objects.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.