Welcome to the comprehensive TypeScript course. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.
-
Getting Started
-
Chapter I
-
Chapter II
-
Chapter III
-
Chapter IV
-
Appendix
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. It was developed and is maintained by Microsoft.
- Static Typing: TypeScript allows you to declare types, catching errors during development.
- Compile to JavaScript: TypeScript code is transpiled to JavaScript.
- Type Inference: TypeScript can infer types even when you don't explicitly declare them.
- Modern Features: TypeScript supports ES6+ features and more.
- Tooling: Enhanced IDE support with autocomplete and error checking.
TypeScript catches errors during development with static type checking.
function add(a: number, b: number): number {
return a + b;
}
add("hello", "world"); // Error!TypeScript provides better autocomplete, inline documentation, and refactoring support.
Many popular frameworks like Angular, React, and Vue support TypeScript.
npm install -g typescript
tsc --versionnpx tsc --init{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
}
}npx tsc
npx tsc --watch
npx ts-node src/index.tslet decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let big: bigint = 100n;let name: string = "John";
let template: string = `Hello, ${name}!`;let isActive: boolean = true;
let isComplete: boolean = false;let numbers: number[] = [1, 2, 3];
let strings: Array<string> = ["a", "b", "c"];let tuple: [string, number];
tuple = ["hello", 42];let anything: any = 4;
anything = "hello";function log(message: string): void {
console.log(message);
}function error(message: string): never {
throw new Error(message);
}let unknownValue: unknown = 4;
if (typeof unknownValue === "number") {
console.log(unknownValue + 2);
}interface User {
name: string;
age: number;
}
const user: User = { name: "John", age: 30 };interface User {
name: string;
age?: number;
}interface User {
readonly id: number;
name: string;
}interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}interface SearchFunc {
(source: string, subString: string): boolean;
}interface Printable {
print(): void;
}
class Document implements Printable {
print() {
console.log("Printing...");
}
}type ID = string | number;
type Point = { x: number; y: number };
type Callback = () => void;enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right // 3
}enum Status {
Success = "SUCCESS",
Error = "ERROR"
}const enum Colors {
Red = "RED",
Green = "GREEN"
}let id: string | number;
id = "abc123";
id = 42;
function padLeft(value: string | number, padding: string | number) {
if (typeof value === "string") {
return value + padding;
}
return String(padding) + value.toString();
}interface Bird {
type: "bird";
flyingSpeed: number;
}
interface Horse {
type: "horse";
runningSpeed: number;
}
type Animal = Bird | Horse;
function move(animal: Animal) {
switch (animal.type) {
case "bird": return animal.flyingSpeed;
case "horse": return animal.runningSpeed;
}
}interface A { a: string; }
interface B { b: number; }
type AB = A & B;
const obj: AB = { a: "hello", b: 42 };function greet(name: string): string {
return `Hello, ${name}!`;
}
function add(a: number, b: number): number {
return a + b;
}function buildName(first: string, last?: string): string {
if (last) return `${first} ${last}`;
return first;
}function greet(name: string = "Guest"): string {
return `Hello, ${name}!`;
}function sum(...numbers: number[]): number {
return numbers.reduce((a, b) => a + b, 0);
}function reverse(x: string): string;
function reverse(x: number): number;
function reverse(x: string | number): string | number {
if (typeof x === "string") {
return x.split("").reverse().join("");
}
return Number(x.toString().split("").reverse().join(""));
}class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet(): string {
return `Hello, ${this.greeting}`;
}
}class Person {
public name: string; // Accessible everywhere
private age: number; // Only in class
protected id: number; // In class and subclasses
readonly createdAt: Date; // Cannot be modified
}class Person {
constructor(
public name: string,
private age: number
) {}
}class Animal {
constructor(public name: string) {}
move(distance: number = 0) {
console.log(`${this.name} moved ${distance}m`);
}
}
class Dog extends Animal {
constructor(name: string, private breed: string) {
super(name);
}
bark(): void {
console.log("Woof!");
}
}abstract class Shape {
abstract getArea(): number;
display(): void {
console.log(`Area: ${this.getArea()}`);
}
}
class Circle extends Shape {
constructor(public radius: number) { super(); }
getArea(): number {
return Math.PI * this.radius ** 2;
}
}function identity<T>(arg: T): T {
return arg;
}
const result = identity<string>("hello");
const num = identity(42);interface Container<T> {
value: T;
getValue(): T;
}class Box<T> {
private content: T;
set(value: T): void { this.content = value; }
get(): T { return this.content; }
}
const box = new Box<number>();
box.set(42);interface Lengthwise {
length: number;
}
function logLength<T extends Lengthwise>(arg: T): number {
return arg.length;
}function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}function pad(value: string | number) {
if (typeof value === "string") {
return value.length;
}
return value.toFixed(2);
}class Dog {
bark(): void { console.log("Woof!"); }
}
class Cat {
meow(): void { console.log("Meow!"); }
}
function speak(animal: Dog | Cat) {
if (animal instanceof Dog) {
animal.bark();
} else {
animal.meow();
}
}function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined;
}type TypeName<T> = T extends string ? "string" : "not string";
type A = TypeName<string>; // "string"
type B = TypeName<number>; // "not string"type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type Func = () => string;
type R = ReturnType<Func>; // stringtype Readonly<T> = {
readonly [P in keyof T]: T[P];
};
type Partial<T> = {
[P in keyof T]?: T[P];
};type Getters<T> = {
[P in keyof T as `get${Capitalize<string & P>}`]: () => T[P];
};Enable in tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true
}
}function sealed(constructor: Function) {
Object.seal(constructor);
}
@sealed
class Person {}function log(target: any, key: string, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = function(...args: any[]) {
console.log(`Calling ${key}`);
return original.apply(this, args);
};
return descriptor;
}// math.ts
export const PI = 3.14;
export function add(a: number, b: number): number {
return a + b;
}
// main.ts
import { PI, add } from "./math";
import * as math from "./math";type User = { name: string; age: number };
type PartialUser = Partial<User>;
// { name?: string; age?: number }type User = { name?: string; age?: number };
type RequiredUser = Required<User>;
// { name: string; age: number }type User = { name: string; age: number; email: string };
type UserPreview = Pick<User, "name" | "email">;type User = { name: string; age: number; email: string };
type UserWithoutAge = Omit<User, "age">;type Page = "home" | "about" | "contact";
type PageInfo = Record<Page, { title: string }>;type T0 = Exclude<"a" | "b" | "c", "a">; // "b" | "c"type T0 = Extract<"a" | "b" | "c", "a" | "f">; // "a"function f(): { a: number; b: string } { return { a: 1, b: "hi" }; }
type T0 = ReturnType<typeof f>;type T0 = Parameters<() => void>; // []
type T1 = Parameters<(s: string) => void>; // [s: string]import React from "react";
interface Props {
name: string;
age?: number;
onClick: (name: string) => void;
}
const UserCard: React.FC<Props> = ({ name, age = 18, onClick }) => {
return (
<div onClick={() => onClick(name)}>
<h1>{name}</h1>
<p>{age} years old</p>
</div>
);
};const [name, setName] = useState<string>("");
const [user, setUser] = useState<User | null>(null);
const [items, setItems] = useState<{ id: number; name: string }[]>([]);const inputRef = useRef<HTMLInputElement>(null);
const countRef = useRef<number>(0);npm install --save-dev @types/nodeimport express, { Request, Response } from "express";
interface User {
id: number;
name: string;
}
const app = express();
app.get("/users/:id", (req: Request, res: Response) => {
const id = parseInt(req.params.id);
res.json({ id, name: "John" } as User);
});Now that you know TypeScript fundamentals:
- Build React apps with TypeScript
- Create Node.js APIs with TypeORM
- Learn testing with Jest
- Explore NestJS framework