Skip to content

Instantly share code, notes, and snippets.

@JimRottinger
Last active May 20, 2019 19:58
Show Gist options
  • Save JimRottinger/0a07570a895b82fcf86e37b8d1a6fcc5 to your computer and use it in GitHub Desktop.
Save JimRottinger/0a07570a895b82fcf86e37b8d1a6fcc5 to your computer and use it in GitHub Desktop.
class Car {
label: string = 'Generic Car'
numWheels: Number = 4
horn() {
return "beep beep!"
}
}
class Truck extends Car {
label = 'Truck'
numWheels = 18
}
class Vespa extends Car {
label = 'Vespa'
numWheels = 2
}
function washCar <T extends Car> (car: T) : T {
console.log(`Received a ${car.label} in the car wash.`)
console.log(`Cleaning all ${car.numWheels} tires.`)
console.log('Beeping horn -', car.horn())
console.log('Returning your car now')
return car
}
const myVespa = new Vespa()
washCar<Vespa>(myVespa)
const myTruck = new Truck()
washCar<Truck>(myTruck)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment