1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
interface Animal { name: string; age: number; type: AnimalType; } enum AnimalType { MAMMAL = "MAMMAL", REPTILE = "REPTILE", BIRD = "BIRD", } function printAnimal<T extends Animal>(animal: T): void { console.log(`Name: ${animal.name}`); console.log(`Age: ${animal.age}`); console.log(`Type: ${animal.type}`); } const dog: Animal = { name: "Buddy", age: 5, type: AnimalType.MAMMAL, }; const bird: Animal = { name: "Tweety", age: 2, type: AnimalType.BIRD, }; printAnimal(dog); printAnimal(bird);
An introduction to TypeScript
I explore why you may want to add TypeScript to your web development toolkit.
February 2023
Development
TypeScript