Skip to main content

Posts

Showing posts with the label What is Optional Properties in TypeScript?

What is Functions in TypeScript? How many types you defined in TypeScript?

A function is a set of statements that perform a specific task and used to create readable, maintainable and re-usable code. A function declaration tells to compiler about a function name, return type, and parameters. TypeScript functions are almost similar to JavaScript functions but there are different ways of writing functions in TypeScript. Stayed Informed – Learn Angular 2 with TypeScript Different types of functions are available in the TypeScript i.e. 1.      Normal function 2.      Anonymous Function 3.      Named Function 4.      Lambda Function/Arrow Function 5.      Class Function 6.      Optional Parameters 7.      Rest Parameters 8.      Default Parameters Anonymous Function s– An anonymous function is a function that was declared without any named identifier to ref...

What is Optional Properties in TypeScript?

We can specify optional properties on interfaces and the property may be present or missing in the object. In the below example, the address property is optional on the following “User” interface. Example as, //USER INTERFACE interface User { name: string ; age: number ; address?: string //Optional } //FUNCTION USING USER INTERFACE let userInfo = function (user: User ) { let info = "Hello, " + user.name + " Your Age is - " + user.age + " and Address is -" + user.address; return info; } //USER INFO JSON OBJECT let info = { name : "Anil" , age: 30 }; //RESULT console.log(userInfo(info)); Stayed Informed – Learn Angular 2 with TypeScript I hope you are enjoying with this post! Please share with you friends. Thank you!!