Created
May 15, 2022 23:22
-
-
Save bennycode/3443627bfab762dd5fa3e200513d9345 to your computer and use it in GitHub Desktop.
Generic Types and Inheritance in TypeScript
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 User { | |
name: string; | |
} | |
interface HappyUser extends User { | |
clap: () => void; | |
} | |
function printName<T extends User>(someone: T): T { | |
console.log(someone.name); | |
return someone; | |
} | |
const benny: HappyUser = { | |
name: 'Benny', | |
clap: (): void => { | |
console.log('👏'); | |
} | |
}; | |
const person = printName(benny); | |
person.clap(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment