Clarvo Blog·Programming

TypeScript in Minutes: Types, Functions, Classes and Interfaces

A fast introduction to TypeScript: installation, compilation, core types, typed functions, classes and interfaces.

Clarvo
Clarvo Editorial Team
8 min read·21 Sept 2026·492 words
TypeScript in Minutes: Types, Functions, Classes and Interfaces
Official TypeScript project icon ↗

1. Start a tiny TypeScript project

Project setupbash
mkdir ts-basics
cd ts-basics
npm init -y
npm install --save-dev typescript
npx tsc --init

A tsconfig.json marks the project root and stores compiler options. Running npx tsc checks and compiles according to that configuration.

2. Primitive types and inference

basics.tsts
let studentName: string = 'Mireille';
let age: number = 19;
let paid: boolean = false;

// TypeScript can infer the type too:
let city = 'Douala'; // string
TypeExampleTypical use
string'Clarvo'Names, labels, IDs when numeric operations are not needed
number42Scores, prices, counts
booleantrueFlags and yes/no state
string[]['React','C']Lists of strings
unknownvalue from an unsafe boundaryForces you to check before use

3. Arrays and object shapes

Typed student datats
type Student = {
  id: string;
  name: string;
  score?: number;
};

const students: Student[] = [
  { id: 's1', name: 'Mireille', score: 16 },
  { id: 's2', name: 'Joel' },
];

4. Functions: type the boundary

Typed functionts
function percentage(score: number, total: number): number {
  if (total <= 0) throw new Error('total must be positive');
  return (score / total) * 100;
}

console.log(percentage(15, 20));

5. Union types

Restricted valuests
type Exam = 'GCE' | 'HND' | 'BTS';

type Learner = {
  name: string;
  exam: Exam;
};

const learner: Learner = { name: 'Amina', exam: 'HND' };

6. Narrowing makes unions useful

Narrow unknown shapests
function formatId(id: string | number) {
  if (typeof id === 'number') {
    return id.toFixed(0);
  }
  return id.toUpperCase();
}

7. Interface versus type

ToolGreat for
interfaceNamed object contracts and extension
typeObjects, unions, tuples, primitives and composition
Interface examplets
interface Product {
  id: string;
  name: string;
  price: number;
}

function label(product: Product) {
  return `${product.name}: ${product.price} FCFA`;
}

8. Generics: keep relationships between types

Generic helperts
function first<T>(items: T[]): T | undefined {
  return items[0];
}

const firstScore = first([12, 14, 18]);      // number | undefined
const firstName = first(['A', 'B', 'C']);    // string | undefined

9. Classes are available, but not mandatory

Small classts
class Wallet {
  balance = 0;

  deposit(amount: number) {
    if (amount <= 0) throw new Error('amount must be positive');
    this.balance += amount;
  }
}

const wallet = new Wallet();
wallet.deposit(500);

10. Compile and run

Compiler loopbash
npx tsc
node dist/index.js

11. Mini-project: typed study planner

planner.tsts
type TaskStatus = 'todo' | 'done';

type StudyTask = {
  id: string;
  title: string;
  subject: string;
  status: TaskStatus;
};

const tasks: StudyTask[] = [];

function addTask(title: string, subject: string): StudyTask {
  const task: StudyTask = {
    id: crypto.randomUUID(),
    title,
    subject,
    status: 'todo',
  };
  tasks.push(task);
  return task;
}

function completeTask(id: string) {
  const task = tasks.find((item) => item.id === id);
  if (task) task.status = 'done';
}

Authoritative next steps

Sources and further reading

Clarvo articles are independently written. These references let you inspect the original inspiration and primary or official sources used to deepen the guide.

  1. Source inspiration — Learn TypeScript in 5 minutesmedium.com
  2. TypeScript official documentationtypescriptlang.org