Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional property class in typescript

I'm new in typescript and I want to know what is the utility of optional property class in typescript? And what is the difference between:

a?: number;  a: number | undefined; 
like image 614
asv Avatar asked Dec 22 '17 13:12

asv


People also ask

What is optional property in TypeScript?

You must tell TypeScript if a property is optional. First, if you don't tell TypeScript that a property is optional, it will expect it to be set. Adding ? to the property name on a type, interface, or class definition will mark that property as optional.

How do you declare optional properties in TypeScript?

When defining a type in TypeScript, we can specify that a property is optional with a question mark after the name: Or we can specify that a property may be undefined: These two interfaces seem nearly identical. In either case, accessing the property foo may return the value undefined .

How do I use optional in TypeScript?

In Typescript, making optional parameters is done by appending the “?” at the end of the parameter name in the function when declaring the parameters and the parameters which are not marked with “?” i.e not optional parameter are called as default parameters or normal parameters where it is must and compulsory to pass ...

How do you add an optional property to an object?

Optional Properties Much of the time, we'll find ourselves dealing with objects that might have a property set. In those cases, we can mark those properties as optional by adding a question mark ( ? ) to the end of their names. In this example, both xPos and yPos are considered optional.


2 Answers

Optional property: In Typescript you can declare a property in your interface which will be optional. Suppose you have a interface for employee and middle name is optional then your code will look like this:

interface IEmployee {   firstName: string;   lastName: string;   middleName?: string; } 

When someone will use your interface IEmployee then middleName will be optional but firstName and lastName is compulsory.

let emp: IEmployee = { firstName: "Hohn", lastName: "Doe" } 

Pipe Operator: Sometimes you want that a variable can hold multiple type. If you declared a property as number then it can hold only number. Pipe operator can tell typescript that it can hold multiple type. Other case where pipe operator is very useful when you return something from function and can return multiple type depend on condition.

Hope it will help

like image 152
Sandip Jaiswal Avatar answered Oct 05 '22 15:10

Sandip Jaiswal


Parameters

The difference between an optional parameter, and a parameter type number | undefined is that you don't have to supply an argument...

function a(a?: number) {     return a; }  function b(a: number | undefined) {     return a; }  // Okay a(); b(1);  // Not okay: Expected 1 arguments, but got 0. b(); 

This applies to functions, methods, and constructors.

Strict Null Checks

If you switch on strict null checks, you'll find that any optional parameter, or property, will get a union type automatically. That means id in the below example has the type number | undefined, even though you only specified number:

// With strict null checks, this: class Example {     id?: number; }  // ... is the same as this: class Example {     id?: number | undefined; }  // ... and this: class Example {     id: number | undefined; } 

I would recommend using the first example as it keeps your syntax consistent between properties and parameters.

like image 32
Fenton Avatar answered Oct 05 '22 16:10

Fenton