YouTip LogoYouTip

TypeScript Types and Generics

Functions with Types

function add(a: number, b: number): number {
    return a + b;
}
const multiply = (a: number, b: number): number => a * b;

Generics

function identity<T>(value: T): T {
    return value;
}
const result = identity<string>("hello");

Classes

class Animal {
    constructor(public name: string) {}
    speak(): string { return `${this.name} speaks`; }
}

Summary

  • Generics enable reusable, type-safe code
  • Classes support public/private modifiers
← Java Tutorial - Getting StarteTypeScript Tutorial - Getting β†’