TypeScript Simple Types
JavaScript has a handful of primitive values: strings, numbers, booleans, big integers, symbols, null and undefined. TypeScript gives each of them a type name you can use in annotations. These simple types are the building blocks for everything else in the course.
The three everyday primitives
| Type | Values | Example |
|---|---|---|
string |
text, in 'single', "double" or `template` quotes |
"Ha Noi" |
number |
all numbers: integers, decimals, negative, NaN, Infinity |
42, 3.14, -7 |
boolean |
true or false |
true |
const city: string = "Da Nang";
const population: number = 1_220_000;
const isCoastal: boolean = true;
console.log(city, population, isCoastal);
console.log(typeof city, typeof population, typeof isCoastal);Da Nang 1220000 true
string number boolean
Note: Always write the type names in lowercase.
String,NumberandBoolean(capitalised) refer to JavaScript's wrapper objects, which you almost never want.
TypeScript has a single number type for both integers and floating-point values, just like JavaScript. There is no separate int or float.
Type errors with primitives
Once a variable has a type, you can only assign values of that type to it. Watch what the compiler says when we break that rule:
let score: number = 10;
score = "eleven";
let ready: boolean = true;
ready = 1;main.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.
main.ts(5,1): error TS2322: Type 'number' is not assignable to type 'boolean'.
TS2322 is the error you'll see most often — "type X is not assignable to type Y". Notice that the compiler reports all problems at once, not just the first one.
The type also controls which operations and methods are allowed. A number has toFixed, a string has toUpperCase — but not the other way round:
const price: number = 19.5;
const label: string = "total";
console.log(label.toUpperCase(), price.toFixed(2));
console.log(price.toUpperCase());main.ts(5,19): error TS2339: Property 'toUpperCase' does not exist on type 'number'.
Remove the last line and the program prints TOTAL 19.50.
bigint and symbol
Two more primitives are less common but good to know:
bigintholds whole numbers of any size. Write them with annsuffix. You can't mixbigintandnumberin arithmetic without converting.symbolcreates unique identifiers, often used as special object keys.
const big: bigint = 9_007_199_254_740_993n;
const doubled: bigint = big * 2n;
console.log(doubled);
const id1: symbol = Symbol("id");
const id2: symbol = Symbol("id");
console.log(id1 === id2, id1.description);18014398509481986n
false id
Mixing them is an error, because JavaScript would throw a TypeError at runtime:
const big: bigint = 10n;
const small: number = 5;
console.log(big + small);main.ts(3,13): error TS2365: Operator '+' cannot be applied to types 'bigint' and 'number'.
The fix is an explicit conversion: big + BigInt(small) gives 15n.
null and undefined
null and undefined also have their own types, named null and undefined. In strict mode (which this course uses), they are not part of other types: a string variable cannot hold null unless you say so. You'll learn how to allow missing values with union types such as string | null in the Null & Undefined lesson.
let nickname: string = "Bin";
nickname = null;main.ts(2,1): error TS2322: Type 'null' is not assignable to type 'string'.
Tip: This single rule —
strictNullChecks, part ofstrictmode — prevents the famous "Cannot read properties of undefined" crash in a huge number of cases.
Summary
| Type | Typical use |
|---|---|
string |
names, messages, IDs |
number |
counts, prices, measurements |
boolean |
flags and conditions |
bigint |
very large integers (IDs, cryptography) |
symbol |
unique keys |
null, undefined |
absence of a value |
Test yourself
Which annotation is correct for a variable that holds 3.75?
- float
- double
- number
- Decimal
Which error code means "Type X is not assignable to type Y"?
- TS2339
- TS2322
- TS2365
- TS1005
Why should you write string and not String in annotations?
Stringis a syntax errorStringrefers to the wrapper object type, not the primitive- They are identical; it's only style
Stringonly works in classes