Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?: mean in Typescript?

Tags:

typescript

I found the following in a TypeScript declaration file for Angular:

interface IDirective{     compile?:         (templateElement: IAugmentedJQuery, 

What does the ?: after compile do?

like image 997
Erik Z Avatar asked May 09 '14 05:05

Erik Z


People also ask

What is ?: operator in TypeScript?

TypeScript 3.7 added support for the ?? operator, which is known as the nullish coalescing operator. We can use this operator to provide a fallback value for a value that might be null or undefined .

What does question mark do in TypeScript?

The question mark ? in typescript is used in two ways: To mention that a particular variable is optional. To pre-check if a member variable is present for an object.

What is '!' In TypeScript?

What is the TypeScript exclamation mark? The non-null assertion operator tells the TypeScript compiler that a value typed as optional cannot be null or undefined . For example, if we define a variable as possibly a string or undefined, the !

What does !: Means in Angular?

The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths.


2 Answers

See: Walkthrough: Interfaces | TypeScript :: Describing Simple Types.

Basically, ? marks the member as being optional in the interface.

(EDIT: As noted in comments, this is not restricted to interfaces.)

like image 117
Amadan Avatar answered Sep 27 '22 18:09

Amadan


In this case, the ?: is not a single operator, but rather two operators:

  • ? (optional),

  • : (specify type).

In other languages/cases, ?: would be the Elvis Operator.

like image 38
KarolDepka Avatar answered Sep 27 '22 18:09

KarolDepka