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 ...

Get Mastering TypeScript 3 - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.