Skip to content

Instantly share code, notes, and snippets.

@jasonbyrne
Created Apr 14, 2022
Embed
What would you like to do?
TypeScript Generics
type FoodProperties = {
name: string;
description: string;
price: number;
section: "breakfast" | "lunch" | "dinner" | "dessert";
};
class FoodItem {
constructor(private opts: FoodProperties) {}
public get<T extends keyof FoodProperties>(property: T): FoodProperties[T] {
return this.opts[property];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment