Skip to content

Instantly share code, notes, and snippets.

@mkulke
Created January 9, 2018 16:46
Show Gist options
  • Save mkulke/15e64d1ef17f80f36467810a7aa8d319 to your computer and use it in GitHub Desktop.
Save mkulke/15e64d1ef17f80f36467810a7aa8d319 to your computer and use it in GitHub Desktop.
contravariant fn argument types
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