Skip to content

Instantly share code, notes, and snippets.

@jasonbyrne
Created April 14, 2022 04:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jasonbyrne/effda1ed28919ac3e9744966da1fb69e to your computer and use it in GitHub Desktop.
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