Learn TypeScript Help

Using type guard

Com verificar se o tipo especifico quando são tipos primitivos podemos usar a keyword typeof que retorna o tipo pertencente a aquele valor

function calculateTax(amount: number, format: boolean): string | number { const calcAmount = amount * 1.2; return format ? `$${calcAmount.toFixed(2)}` : calcAmount; } let taxValue = calculateTax(100, false); if (typeof taxValue === "number") { console.log(`Number Value: ${taxValue.toFixed(2)}`); } else if (typeof taxValue === "string") { console.log(`String Value: ${taxValue.charAt(0)}`); }

Usando o Type Guard com switch

function calculateTax(amount: number, format: boolean): string | number { const calcAmount = amount * 1.2; return format ? `$${calcAmount.toFixed(2)}` : calcAmount; } let taxValue = calculateTax(100, false); switch (typeof taxValue) { case "number": console.log(`Number Value: ${taxValue.toFixed(2)}`); break; case "string": console.log(`String Value: ${taxValue.charAt(0)}`); break; }
11 November 2025