A boolean represents one of two values: true or false. JavaScript infers boolean values from assignments, while TypeScript can use boolean assertions and checks to describe them. section 7.2 section 7.4.1
Observing a boolean value
let myVar;\nconsole.log(`${myVar} = ${typeof myVar}`);\nmyVar = true;\nconsole.log(`${myVar} = ${typeof myVar}`);\n\n// undefined = undefined\n// true = boolean
Boolean behavior matters when JavaScript compares or combines values. Strict equality (===) compares both type and value, whereas logical OR (||) converts several values to false and may select a fallback. section 3.3.2
Treat fallback expressions carefully:
- Use || when values such as null or undefined should trigger a fallback, but remember that false, 0, an empty string, and NaN also trigger it. section 3.3.2
- Do not confuse a TypeScript assertion with conversion: asserting a string | number result as boolean changes checking only, not the runtime value. section 7.4.1
An unsafe boolean assertion
function calculateTax(amount: number, format: boolean): string | number {\n const calcAmount = amount * 1.2;\n return format ? `$${calcAmount.toFixed(2)}` : calcAmount;\n}\n\nlet taxBoolean = calculateTax(100, false) as any as boolean;\nconsole.log(taxBoolean); // 120










