- 01Getting Started
- 02Your First Output (console.log)
- 03Variables and Type Annotations (number/string/boolean)
- 04Array Types
- 05Function Types (Parameters and Return Values)
- 06Optional Parameters and Default Parameters
- 07Interfaces (interface)
- 08Optional Properties (?)
- 09Type Aliases (type)
- 10Union Types
- 11Literal Types
- 12enum (Enumerated Types)
- 13Classes and Types
- 14Inheritance (extends) and Implementing an Interface (implements)
- 15Generics (<T>)
- 16Tuple Types
- 17The any and unknown Types
- 18Type Assertions (as)
- 19The readonly Modifier
- 20[Applied] Build a Mini Inventory Management System
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.
๐งช 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).
