Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generic Types and Inheritance in TypeScript
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