Created
January 9, 2018 16:46
-
-
Save mkulke/15e64d1ef17f80f36467810a7aa8d319 to your computer and use it in GitHub Desktop.
contravariant fn argument types
This file contains hidden or 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 Vehicle { | |
color: string; | |
} | |
interface Car extends Vehicle { | |
drive: () => void; | |
} | |
interface Bike extends Vehicle { | |
peddle: () => void; | |
} | |
function noop() { | |
return; | |
} | |
type VehicleActionFn = (v: Vehicle) => void; | |
function doSomething(action: VehicleActionFn): void { | |
const car: Car = { color: 'green', drive: noop }; | |
action(car); | |
} | |
function peddleBike(bike: Bike): void { | |
bike.peddle(); | |
} | |
// doSomething(peddleBike as VehicleActionFn); | |
doSomething(peddleBike); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment