Skip to content

Instantly share code, notes, and snippets.

@thathurtabit
Created May 1, 2021 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thathurtabit/48efd800059a76cffea6a679f4a98103 to your computer and use it in GitHub Desktop.
Save thathurtabit/48efd800059a76cffea6a679f4a98103 to your computer and use it in GitHub Desktop.
TypeScript Generics Example (by @wesbos)
// This function takes a Generic of FoodType
function makeABunchOf<FoodType>(food: FoodType, howMany: number): FoodType[] {
const foodArray = Array.from({ length: howMany }, () => food);
return foodArray;
}
// Make our Food Types
interface Pizza { name: string; slices: number; }
interface Sandwich { name: string; veggie: boolean; }
// Make some Food
const hamSandwich: Sandwich = { name: 'Hammy Sammy', veggie: false, };
const canadianPizza: Pizza = { name: 'Canadian Pizza', slices: 10, };
// Here we pass the FoodType to the generic explicitly
const pizzaExplicit = makeABunchOf<Pizza>(canadianPizza, 3); // type of Pizza
// On this one, we infer a <FoodType> of Pizza, because we pass a pizza
const pizzas = makeABunchOf(canadianPizza, 3); // type of Pizza
// Here Sandwich type in inferred because we pass a Sandwich
const sandwiches = makeABunchOf(hamSandwich, 3); // type of Sandwich
// this is also good if passing the values directly as they can't be inferred
const pizzaExplicit2 = makeABunchOf<Pizza>({ slices: 11, name: 'Pepperoni', }, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment