// sample-type-error.ts: THIS FILE CONTAINS ONE DELIBERATE TYPE ERROR. // Free sample from 99helpers.com/file-types/ts // // Use it to see the difference between running and type-checking: // // node sample-type-error.ts runs, and prints "Total: 1020" (wrong) // npx tsx sample-type-error.ts same // npx tsc --noEmit sample-type-error.ts rejects it: // error TS2345: Argument of type 'string' is not assignable to // parameter of type 'number'. // // Runtimes only remove the types; only the type checker reads them. function total(a: number, b: number): number { return a + b; } // Text from a form field is a string, even when it looks like a number. const fromForm = "10"; // The deliberate error: a string passed where a number is expected. // JavaScript then joins "10" and 20 as text instead of adding them. console.log(`Total: ${total(fromForm, 20)}`); // The fix: convert first. This line prints "Total: 30". console.log(`Total: ${total(Number(fromForm), 20)}`);