TypeScript · Lesson 6 of 42
TypeScript primitive types: string, number, boolean and friends
Meet the primitive types string, number, boolean, bigint, symbol, null and undefined, and learn why you should use lowercase type names.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will know every primitive type TypeScript has, which ones you will use daily, and the rules that surprise beginners: no separate integer type, no mixing bigint with number, and null and undefined that behave as their own types.
The seven primitives
A primitive is a simple value that is not an object. TypeScript has a type for each of JavaScript's primitives:
| Type | Holds | Example |
|---|---|---|
string |
text | "hello" |
number |
integers and decimals | 42, 3.14 |
boolean |
true or false | true |
bigint |
very large whole numbers | 9007199254740993n |
symbol |
a unique identifier | Symbol("id") |
null |
an intentional empty value | null |
undefined |
a value that was never set | undefined |
Always write these names in lowercase. TypeScript also has String, Number and Boolean with capital letters, but those describe the wrapper objects of JavaScript. You almost never want them, and using them is a common beginner slip.
string, number and boolean
These three cover most everyday values:
const name: string = "Asha";
const price: number = 19.99;
const quantity: number = 3;
const inStock: boolean = true;
const total: number = price * quantity;
console.log(`${name}: ${total.toFixed(2)} (in stock: ${inStock})`);Asha: 59.97 (in stock: true)Notice that price and quantity are both number. There is no int or float. Whole numbers and decimals share one type, and the values NaN and Infinity are numbers too. You can check what JavaScript thinks a value is with typeof:
console.log(typeof "text", typeof 42, typeof true, typeof 10n, typeof Symbol("x"), typeof undefined, typeof null);string number boolean bigint symbol undefined objectThe last result is a famous quirk: typeof null is "object". That is a long-standing JavaScript oddity, not a TypeScript rule. TypeScript treats null as its own type.
bigint
A number stores whole numbers exactly only up to a limit, about nine quadrillion. Beyond it, precision is lost. A bigint has no such limit and is written with a trailing n:
const big: bigint = 9007199254740993n;
console.log(big + 2n);
console.log(Number.MAX_SAFE_INTEGER);9007199254740995n
9007199254740991The two types do not mix. TypeScript catches the mistake before JavaScript would throw at run time:
const big: bigint = 10n;
const total = big + 5;example.ts(2,15): error TS2365: Operator '+' cannot be applied to types 'bigint' and '5'.Convert one side explicitly, for example big + BigInt(5). Use bigint only when you truly need huge exact integers, such as some identifiers or cryptography; ordinary counting and money in small units fit a number.
symbol
A symbol is a value that is guaranteed to be unique. Beginners rarely need it, but you will see it in library code:
const a: symbol = Symbol("id");
const b: symbol = Symbol("id");
console.log(a.toString(), a === b);Symbol(id) falseTwo symbols with the same description are still different values.
null and undefined
undefined means "no value has been set", for example a variable that was declared but never assigned, or a missing object property. null means "deliberately empty". With the strict setting on, which this course uses, they are not assignable to other types:
const username: string = null;example.ts(1,7): error TS2322: Type 'null' is not assignable to type 'string'.This protects you from one of the most common bugs in JavaScript, calling a method on null or undefined. If a value can be missing, say so with a union, which you will meet in detail soon:
let nickname: string | null = null;
console.log(nickname);
nickname = "Ash";
console.log(nickname.toUpperCase());null
ASHHere string | null means "either a string or null". After the assignment, the compiler knows nickname is a string, so calling toUpperCase is allowed.
Common mistakes
Using capital-letter types. Write string, number and boolean, not String, Number and Boolean.
Looking for int and float. There is only number. If you need whole numbers, check them in your code; the type does not enforce it.
Mixing bigint and number. Convert explicitly, and notice that large bigint values lose precision if converted back to number.
Treating null and undefined as the same. They are separate types. Decide which one your code uses for "missing" and stay consistent.
Turning off strict null checks to make errors disappear. The errors are pointing at real places where a value might be missing.
Exercise
Declare one variable of each primitive type with an annotation. Print typeof for each. Then try three things that should fail and read the messages: assign a string to a number, add a bigint and a number, and assign null to a string. For the last one, fix the type with string | null and write an if check before using the value.
Next: Typing arrays and tuples.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.