Saturday, November 3, 2018

TypeScript - DataTypes - type alias for objects

In the previous example, object creation was very cluttered as we have the object type and its blueprint combined. If we want to create a object type blueprint as a reusable component, then consider the following syntax:

// create type
type myComplexObjectTemplate={dataArray:number[], outputFunc:(all:boolean) => number[]};

This creates a blue print for object creation. Which we can use as follows:

// create type
type myComplexObjectTemplate={dataArray:number[], outputFunc:(all:boolean) => number[]};
let myComplexObject2:myComplexObjectTemplate ={
dataArray:[10,20,30,40,50],
outputFunc: function (all:boolean): number[] {
return this.dataArray;
}
};


No comments:

Post a Comment