Created
February 2, 2022 10:45
-
-
Save isabellachen/30b4ee2ab6d3a47bce9a6d21ec26d00a to your computer and use it in GitHub Desktop.
TypeScript Series: Generics and Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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