Skip to content
Laviyx Learn

Try

TypeScript course menu

TypeScript Basic Types: string, number, boolean, null, undefined and More

Learn TypeScript's basic types and annotations: string, number, boolean, bigint, symbol, null and undefined, annotating variables, parameters and return values, void, the difference between string and String, and type errors you will see.

Beginner5 min readUpdated
On this page

TypeScript's types mirror JavaScript's values. This lesson covers the primitive types and how to write annotations.

The primitive types#

TypeValuesExample
stringText"Asha", `Hi ${name}`
numberAll numbers, whole and decimal42, 3.14, NaN
booleantrue or falseisActive
bigintLarge integers10n
symbolUnique identifiersSymbol("id")
nullIntentional absencenull
undefinedNot setundefined
TypeScript
const name: string = "Asha Rao";
const age: number = 21;
const isEnrolled: boolean = true;
const population: bigint = 1_400_000_000n;

console.log(name, age, isEnrolled, population);
Output
Asha Rao 21 true 1400000000n

Annotation syntax#

The type goes after a colon, in three main places:

TypeScript
let score: number = 0;                          // variable

function greet(name: string): string {          // parameter and return type
  return `Hello, ${name}!`;
}

const shout = (text: string): string => text.toUpperCase() + "!";

score += 10;
console.log(greet("Ravi"), shout("go"), score);
Output
Hello, Ravi! GO! 10

Type errors#

A variable keeps its type for its whole life. Assigning a different type is an error:

TypeScript
let score: number = 0;
score = "ten";
Compiler error
error TS2322: Type 'string' is not assignable to type 'number'.

Using a method that does not exist for the type is an error too:

TypeScript
const age: number = 21;
console.log(age.toUpperCase());
Compiler error
error TS2339: Property 'toUpperCase' does not exist on type 'number'.

void: functions that return nothing#

A function that does not return a value has the return type void:

TypeScript
function logVisit(page: string): void {
  console.log(`Visited ${page}`);
}

logVisit("/typescript");
Output
Visited /typescript

null and undefined#

With strict mode on (as it should be), null and undefined are not allowed in other types unless you say so:

TypeScript
let middleName: string = null;
Compiler error
error TS2322: Type 'null' is not assignable to type 'string'.

To allow them, use a union type with |, covered in Union Types:

TypeScript
let middleName: string | null = null;
middleName = "Kumar";
console.log(middleName);
Output
Kumar

This is one of TypeScript's most valuable checks: it forces you to handle "missing" values, the source of countless JavaScript errors like "Cannot read properties of undefined". See null, undefined and Strict Null Checks.

string, not String#

Use the lowercase types: string, number, boolean. The capitalised String, Number and Boolean refer to JavaScript's wrapper objects, which you almost never want:

TypeScript
const good: string = "text";
console.log(typeof good);
Output
string

Practice#

Practice exercise

Annotate everything in this snippet, then fix the line that the compiler would reject:

TypeScript
const product = "Notebook";
let stock = 40;
const isAvailable = stock > 0;
function restock(amount) {
  stock = stock + amount;
}
restock("10");
Show solution
TypeScript
const product: string = "Notebook";
let stock: number = 40;
const isAvailable: boolean = stock > 0;

function restock(amount: number): void {
  stock = stock + amount;
}

restock(10);
console.log(product, stock, isAvailable);
Output
Notebook 50 true

In the original, restock("10") would have joined strings ("4010") at runtime. With amount: number, the compiler rejects it.

Key takeaway#

Finished this lesson?