Skip to Main Content
Mastering TypeScript 3 - Third Edition
book

Mastering TypeScript 3 - Third Edition

by Nathan Rozentals
February 2019
Beginner content levelBeginner
694 pages
18h 4m
English
Packt Publishing
Content preview from Mastering TypeScript 3 - Third Edition

Conditional types

One of the features of the TypeScript language includes a simple, streamlined version of an if then else statement, which uses the question mark (?) symbol to define the if statement, and the colon symbol (:) to define the then and else path. These are called conditional statements. The format of the conditional statement is as follows:

(conditional statement) ? (true value) : (false value);

We start with a conditional statement followed by a ? symbol. We then define the true value and the false value separated by the : symbol. As an example of this, consider the following code:

let trueValue = true; 
let printValue = trueValue === true ? "true" : "false"; 
 
console.log(`printValue is : ${printValue}`); 

Here, we have defined ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The TypeScript Workshop

The TypeScript Workshop

Ben Grynhaus, Jordan Hudgens, Rayon Hunte, Matt Morgan, Wekoslav Stefanovski

Publisher Resources

ISBN: 9781789536706Supplemental Content