February 2019
Beginner
694 pages
18h 4m
English
TypeScript will also check for null or undefined values when we use basic operands, such as addition, multiplication, less than, modulus, and power of. This can best be seen by using a simple example, as follows:
function testNullOperands(arg1: number, arg2: number | null | undefined) {
let a = arg1 + arg2;
let b = arg1 * arg2;
let c = arg1 < arg2;
}
Here, we have defined a function named testNullOperands that takes two arguments, arg1 and arg2, which can either be number, null, or undefined. Attempting to compile this code will result in a number of errors, as follows:
(92,20): error TS2533: Object is possibly 'null' or 'undefined' (93,20): error TS2533: Object is possibly 'null' or 'undefined' (94,20): error TS2533: Object ...
Read now
Unlock full access