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

TS basics.ts
let username: string = "jordan";
let age: number = 29;
let isAdmin: boolean = false;

console.log(username, age, isAdmin);
Output
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.

TypeExample valuesNotes
string"hi", 'hi', `hi ${name}`All three quote styles produce a string
number42, 3.14, -7, 0xff, 1_000_00064-bit floating point, like JavaScript
booleantrue, falseOnly those two values
bigint9007199254740993nWhole numbers of any size; note the n
symbolSymbol("id")Unique identifiers, rarely needed
null, undefinednull, undefinedCovered 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:

TS wrappers.ts
let good: string = "fine";
let wrapper: String = "works, but don't";

good = wrapper;
tsc output
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:

TS mismatch.ts
let price: number = 19.99;
price = "free";

let inStock: boolean = true;
inStock = 1;
tsc output
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:

TS big.ts
const safeMax: number = Number.MAX_SAFE_INTEGER;
const huge: bigint = 9007199254740993n;

console.log(safeMax + 2);
console.log(huge + 1n);
Output
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

Try it yourself
Console output
Note: TypeScript also has 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.