Start using TypeScript
Read a TypeScript error
Turn a compiler diagnostic into three concrete facts: the received type, the expected type, and the location where they conflict.
A TypeScript error usually gives you three useful facts:
- The type TypeScript found.
- The type the receiving code requires.
- The source location where those types meet.
Example:
function setPort(port: number) {}
setPort('3000')
The important part of the diagnostic is: string is not assignable to number.
Start at the first error in your own source. Later errors can be consequences of that one. Hover over the argument and parameter to inspect what TypeScript inferred.
Then decide which side is wrong. If the value came from an environment variable, parse and validate it. If the function should accept text, change the contract deliberately.
Do not reach immediately for any, as number, or an ignore comment. Those silence evidence without fixing the value.
Exercise: make setPort() accept only numbers from 1 through 65535. Notice which part needs a type and which part needs a runtime check.
Lesson completed