Why add types?
In plain JavaScript this mistake only shows up when the code runs, and maybe only for some users:
function total(price, quantity) {
return price * quantity;
}
total("10", 3); // 30, because "10" is quietly converted
total(10); // NaN: quantity is undefined
TypeScript lets you say that both inputs must be numbers. Your editor then underlines the wrong calls straight away. When the code is ready, TypeScript is compiled (translated) into ordinary JavaScript that any browser can run.
Type annotations
Write a colon and a type after a variable or parameter name. The basic types are string, number and boolean:
let city: string = "Tirana";
let year: number = 2026;
let isOpen: boolean = true;
function total(price: number, quantity: number): number {
return price * quantity;
}
console.log(city, year, isOpen);
console.log(total(10, 3)); // 30
The : number after the brackets is the return type. Calling total("10", 3) now gives an error: Argument of type 'string' is not assignable to parameter of type 'number'.
You do not have to annotate everything. TypeScript infers types from values, so let score = 0; is already a number.
Arrays and objects
An array type is the item type followed by []. To describe the shape of an object, use an interface:
interface Student {
name: string;
grade: number;
email?: string; // the ? means optional
}
const students: Student[] = [
{ name: "Arta", grade: 8 },
{ name: "Dren", grade: 9, email: "[email protected]" },
];
for (const s of students) {
console.log(`${s.name} is in grade ${s.grade}`);
}
If you misspell a property, for example s.grdae, TypeScript reports Property 'grdae' does not exist on type 'Student' instead of quietly giving you undefined.
Union types
A union says a value can be one of several types or exact values. TypeScript then makes you handle each case:
type Status = "draft" | "submitted" | "graded";
function label(status: Status): string {
if (status === "draft") return "Not handed in yet";
if (status === "submitted") return "Waiting for the teacher";
return "Marked";
}
console.log(label("submitted"));
function formatId(id: number | string): string {
return typeof id === "number" ? id.toFixed(0) : id.toUpperCase();
}
console.log(formatId(42), formatId("abc"));
Inside each branch TypeScript knows which type you have, so toFixed is only allowed on the number and toUpperCase only on the string. This is called narrowing.
Reading TypeScript errors
- "is not assignable to type": you passed or stored the wrong kind of value. Check the expected type in the message.
- "Object is possibly 'undefined'": a value might be missing, for example an optional property. Check it first with
if (student.email) { ... }. - "Property does not exist": usually a typo, or the object really does not have that field.
Avoid silencing errors with the any type. It switches type checking off for that value, which hides the very mistakes TypeScript is there to catch.
Try it yourself
In Unays Code, choose TypeScript and write an interface Book with a title, an author and an optional year. Make an array of three books and print each one. Then call a function with the wrong type on purpose and read the error. If JavaScript itself is new to you, start with JavaScript basics.
This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].