Ad space (banner)
Lesson 3 / 20

Variables and Type Annotations (number/string/boolean)

This lesson covers TypeScript's basic types โ€” number, string, and boolean. It's for anyone searching "TypeScript type annotation basics."

TypeScript has three main basic types: number, string, and boolean. Attaching : type-name when declaring a variable restricts what kind of value can go into it.

The example declares three variables โ€” name: string, age: number, and isStudent: boolean โ€” and builds a message using a template literal (the `${}` syntax).

A common early mistake is trying to assign a value of the wrong type and getting an error. For example, trying to put the string "14" into age: number gets flagged as a compile-time error โ€” this "catching bugs before you even run the code" is one of TypeScript's biggest benefits.

In real projects, this type checking makes it much easier to prevent the bug of "an unexpectedly-typed value showing up," even on a large team.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)