Skip to content

Instantly share code, notes, and snippets.

@isabellachen
Created February 2, 2022 10:45
Show Gist options
  • Save isabellachen/30b4ee2ab6d3a47bce9a6d21ec26d00a to your computer and use it in GitHub Desktop.
Save isabellachen/30b4ee2ab6d3a47bce9a6d21ec26d00a to your computer and use it in GitHub Desktop.
TypeScript Series: Generics and Classes
interface HotelRooms {
name: string;
capacity: number;
}
class Hotel<T extends HotelRooms> {
public rooms: T[] = []
addRoom(room:T) {
this.rooms.push(room)
}
getBigRooms(minSize: number): T[] {
const bigRooms: T[] = []
this.rooms.forEach(room => {
if (room.capacity > minSize) {
bigRooms.push(room)
}
})
return bigRooms
}
}
const hotelRooms = new Hotel()
hotelRooms.addRoom({name:'hibicus suite', capacity: 4})
hotelRooms.addRoom({name:'jacana penthouse', capacity: 6})
hotelRooms.addRoom({name:'frangipani villa', capacity: 12})
hotelRooms.addRoom({name:'daisy double', capacity: 2})
const bigRooms = hotelRooms.getBigRooms(5)
console.log(bigRooms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment