In this lesson you will
- Read a TypeScript error message and find the line and the cause
- Annotate variables, arrays, parameters and return values with basic types
- Let inference do the work and know when an annotation helps
- Handle values that may be null, and choose unknown over any
Your first type error
A type is the kind of value something holds: text, a number, true or false, a list. When you write const qty: number = 2, the part after the colon is a type annotation. It is a promise: qty will always be a number. If you break the promise, for example by writing "2" with quotes, the compiler stops you with a message such as "Type 'string' is not assignable to type 'number'". Read it from the inside out: you tried to put a string where a number was promised.
Each message has a code, such as TS2322, and a line number. The codes are useful for searching, and the most common ones soon become familiar: TS2322 for a value of the wrong type, TS2345 for a wrong argument in a function call, TS2339 for a property that does not exist and TS18047 for a value that may be null. Notice in the example that the JavaScript still runs after the error, and prints the wrong total. The compiler warned you; it did not stop you. In a real project you fix every error before you publish.
Console
- Console messages, errors, form data and clicked links appear here.
The Console shows TS2322 on line 5, then the JavaScript runs anyway and prints 203 instead of 23, because the string was glued on. Remove the quotes around 3 and press Run: the error disappears and the total is right.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between any and unknown in TypeScript?
Both accept any value. With any, TypeScript stops checking and lets you do anything with it, including mistakes. With unknown, you must first check the value, for example with typeof, before TypeScript lets you use it. Prefer unknown.
Is there an integer type in TypeScript?
No. Like JavaScript, TypeScript has one number type for whole numbers and decimals. For whole numbers larger than about 9 quadrillion there is bigint. Check for whole numbers at run time with Number.isInteger().
What does the exclamation mark after a variable mean in TypeScript?
It is the non-null assertion: el! tells TypeScript the value is not null or undefined. It removes the type error but adds no check, so if you are wrong the code still crashes. Use an if check instead whenever you can.
Why does the JavaScript still run when there are type errors?
By default the TypeScript compiler reports errors but still writes the JavaScript, so a type error never blocks you while you experiment. Projects usually stop the build on errors with the noEmitOnError option or a separate check step in their tests.
Do I have to rewrite my JavaScript project to use TypeScript?
No. Because every JavaScript file is valid TypeScript syntax, you can rename files from .js to .ts one at a time and add types gradually. The allowJs option lets both kinds of file live in the same project while you move over.
Sources
Facts in this lesson were checked against these official pages on 17 November 2026.
- TypeScript Handbook: The Basics
- TypeScript Handbook: Everyday Types
- TypeScript Handbook: Type Inference
- TSConfig reference: strict
- TSConfig reference: strictNullChecks
- Node.js: Running TypeScript natively
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.