Start using TypeScript

Types do not change runtime behavior

Understand that annotations are erased and cannot validate a network response, form value, or JSON file while the program runs.

This assertion tells TypeScript to trust you:

const user = (await response.json()) as User

It does not inspect the response body. The server can still return null, an error object, or a user with missing fields.

Types also do not convert values:

const port = process.env.PORT as unknown as number

At runtime, an environment variable is still a string. The assertion changes only what the checker believes.

Treat values from JSON, forms, storage, environment variables, and network calls as uncertain. Check them at the boundary before using a more specific type.

For a user, that means verifying the value is an object and checking the fields your program needs. A schema library can help, but ordinary JavaScript checks work too.

Exercise: log typeof process.env.PORT at runtime. Compare that result with what an unsafe assertion can make TypeScript claim.

Lesson completed

Take this course offline

Get every free book, course edition, and software download.

Get the download library →