Skip to content

Instantly share code, notes, and snippets.

@softwarebygabe
Last active June 22, 2020 20:36
Show Gist options
  • Save softwarebygabe/275f8f81c3de52a2711c6acf4f2c9fee to your computer and use it in GitHub Desktop.
Save softwarebygabe/275f8f81c3de52a2711c6acf4f2c9fee to your computer and use it in GitHub Desktop.
type Material = 'wood' | 'brick' | 'steel'
interface HouseOptions {
rooms: number
floors: number
material: Material
bathrooms: number
squareFootage: number
address: string
constructionDate?: Date
}
class House {
private rooms: number
private floors: number
private material: Material
private bathrooms: number
private squareFootage: number
private address: string
private constructionDate: Date
constructor(options: HouseOptions) {
if (options.rooms < 1) {
throw new Error('invalid number of rooms!')
}
this.rooms = options.rooms
if (options.floors < 1) {
throw new Error('invalid number of floors!')
}
this.floors = options.floors
this.material = options.material
// handling of defaults and validation
if (options.constructionDate === undefined) {
this.constructionDate = new Date()
} else {
if (options.constructionDate > (new Date())) {
throw new Error('construction date cannot be in the future!')
}
this.constructionDate = options.constructionDate
}
// etc ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment