Basic Types
TypeScript's everyday types map directly onto JavaScript's primitive values: text, numbers, true/false, and a couple of rarer ones. Learning to write them is the first step; learning when you don't need to write them comes in the next lesson.
The core primitives
let username: string = "jordan"; let age: number = 29; let isAdmin: boolean = false; console.log(username, age, isAdmin);
jordan 29 false
The syntax is always name: type. Unlike C or Java, TypeScript only has one numeric type, number, covering integers, decimals, negative values, Infinity and NaN — there's no separate int or float.
| Type | Example values | Notes |
|---|---|---|
string | "hi", 'hi', `hi ${name}` | All three quote styles produce a string |
number | 42, 3.14, -7, 0xff, 1_000_000 | 64-bit floating point, like JavaScript |
boolean | true, false | Only those two values |
bigint | 9007199254740993n | Whole numbers of any size; note the n |
symbol | Symbol("id") | Unique identifiers, rarely needed |
null, undefined | null, undefined | Covered in lesson 13 |
Lowercase, not uppercase
Always use the lowercase names. String, Number and Boolean with capital letters also exist, but they refer to JavaScript's wrapper objects, which almost no code wants:
let good: string = "fine"; let wrapper: String = "works, but don't"; good = wrapper;
error TS2322: Type 'String' is not assignable to type 'string'.
Mixing the two causes confusing errors like this one, which is why style guides and linters ban the uppercase forms.
Mismatches are caught everywhere
Once a variable has a type, every later assignment is checked against it:
let price: number = 19.99; price = "free"; let inStock: boolean = true; inStock = 1;
error TS2322: Type 'string' is not assignable to type 'number'. error TS2322: Type 'number' is not assignable to type 'boolean'.
The second error surprises people who know JavaScript, where 1 is "truthy". TypeScript is stricter: a boolean slot only accepts true or false.
bigint for very large integers
number can only represent integers exactly up to 253 − 1. Beyond that, use bigint:
const safeMax: number = Number.MAX_SAFE_INTEGER; const huge: bigint = 9007199254740993n; console.log(safeMax + 2); console.log(huge + 1n);
9007199254740992 9007199254740994n
The number result is off by one because it lost precision. The bigint result is exact. You can't mix the two in arithmetic without converting — huge + 1 is a compile error.
Try it yourself
any, which switches type checking off for a value, and unknown, a safer alternative for values you can't describe yet. They're important enough to get their own lesson — see lesson 05.