Skip to content

Instantly share code, notes, and snippets.

@AlvisonHunterArnuero
Created November 14, 2024 06:07
Show Gist options
  • Save AlvisonHunterArnuero/ba68a7c9c5e8bbe9ffd8bf336a4610b7 to your computer and use it in GitHub Desktop.
Save AlvisonHunterArnuero/ba68a7c9c5e8bbe9ffd8bf336a4610b7 to your computer and use it in GitHub Desktop.
/* ======================== EXAMPLE ONE ========================
| Made with ❤️ with TypeScript by Alvison Hunter - Nov 13th, 2024
* =============================================================
*/
const productPrices = {
Apple: 1.5,
Banana: 0.5,
Orange: 1,
Mango: 2.5,
};
/**
* Retrieves the price of a product.
*
* @param {Extract<keyof typeof productPrices, string>} productName - The name of the product to get the price for.
* @returns {number} The price of the specified product.
*/
const getPrice = (productName: Extract<keyof typeof productPrices, string>): number => productPrices[productName];
console.log(getPrice("Apple"));
console.log(getPrice("Banana"));
console.log(getPrice("Mango"));
/* ======================== EXAMPLE TWO ========================
| Made with ❤️ with TypeScript by Alvison Hunter - Nov 13th, 2024
* =============================================================
*/
const PlayersWithMostBallonDOr = {
"Leonel Messi": 8,
"Cristiano Ronaldo": 5,
"Michel Platini": 3,
"Ronaldo Fenomeno": 2,
};
type PlayerNames = keyof typeof PlayersWithMostBallonDOr;
/**
* Retrieves the number of Ballon d'Or awards a player has won.
*
* @param {PlayerNames} playerName - The name of the player to query.
* @returns {number} The number of Ballon d'Or awards the player has won.
*/
const getPlayerWithMostBallonDOr = (playerName: PlayerNames): number => {
return PlayersWithMostBallonDOr[playerName];
};
console.log(getPlayerWithMostBallonDOr("Leonel Messi"));
console.log(getPlayerWithMostBallonDOr("Cristiano Ronaldo"));
console.log(getPlayerWithMostBallonDOr("Michel Platini"));
console.log(getPlayerWithMostBallonDOr("Ronaldo Fenomeno"));
/* ======================= EXAMPLE THREE =======================
| Made with ❤️ with TypeScript by Alvison Hunter - Nov 13th, 2024
* =============================================================
*/
const pizzaPrices = {
Margherita: 8.99,
Pepperoni: 10.99,
BBQChicken: 12.49,
Veggie: 9.99,
Hawaiian: 11.49,
} as const;
type PizzaName = "Margherita" | "Pepperoni" | "BBQChicken" | "Veggie" | "Hawaiian";
/**
* Function to get the price of a pizza
* @param pizzaName - Name of the pizza (must be a key of pizzaPrices)
* @param isDelivery - Whether the pizza is for delivery (adds delivery charge if true)
* @returns The price of the pizza, including delivery charge if applicable
*/
const getPizzaPrice = (pizzaName: PizzaName, isDelivery: boolean): number => {
const basePrice = pizzaPrices[pizzaName];
const deliveryCharge = isDelivery ? 3.5 : 0.0;
return basePrice + deliveryCharge;
}
console.log(getPizzaPrice("Margherita", false));
console.log(getPizzaPrice("Pepperoni", true));
console.log(getPizzaPrice("Hawaiian", false));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment