Skip to content

Instantly share code, notes, and snippets.

@jaycosaur
Created August 9, 2020 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaycosaur/5ee27791cf991dee5939370ba416f270 to your computer and use it in GitHub Desktop.
Save jaycosaur/5ee27791cf991dee5939370ba416f270 to your computer and use it in GitHub Desktop.
Composable types [typescript] - Typescript to Python field guide
interface Animal {
length: number;
eat: (food: string) => void;
}
// Fish extends the Animal interface
interface Fish extends Animal {
swim: (howLong: number) => void;
}
// fish implements the Fish interface implicitly and does not directly reference it
const fish = {
length: 12,
eat: (food: string) => console.log(`Eating ${food}`),
swim: (howLong: number) => console.log(`Swimming for ${howLong}`),
};
function doSomethingFishy(what: Fish) {
what.eat("worms");
what.swim(what.length);
}
doSomethingFishy(fish); // this is ok!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment