Narrowing Types
function padLeft(padding: number | string, input: string) {
return " ".repeat(padding) + input;
}
/*
Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
*/function padLeft(padding: number | string, input: string) {
if (typeof padding === "number") {
return " ".repeat(padding) + input;
}
return padding + input;
}Guards
Typeof
Boolean
instanceof
Advanced Types => Type predicates
Last updated