Union type
Joint type represents a value can be one of several types. We use a vertical bar (|) to separate each type, the number | string | boolean represents a value can be a number, string, or boolean.
function padLeft(value: string, padding: string | number) {
// ...
}
let indentedString = padLeft("Hello world", true); // errors during compilation
Use the --strictNullChecks (:), optional parameters are automatically plus | undefined?:
function f(x: number, y?: number) {
return x + (y || 0);
}
f(1, 2);
f(1);
f(1, undefined);
f(1, null); // error, 'null' is not assignable to 'number | undefined'