Created
August 9, 2020 11:48
-
-
Save jaycosaur/5ee27791cf991dee5939370ba416f270 to your computer and use it in GitHub Desktop.
Composable types [typescript] - Typescript to Python field guide
This file contains 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 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