TypeScript等值縮小
function example (x: string | number,y: string | boolean) {
if (x === y) {
x.toUpperCase();
y.toUpperCase();
} else {
console.log(x);
console.log(y);
}
}
出去變量中null的情況:
function printAll(strs: string | string[] | null) {
if (strs !== null) { // 去除null
if (typeof strs === "object") {
for (const s of strs) {
console.log(s);
}
}
}
}