typehero typescript challenge Day Two - Christmas Cookie Inventory - Advent of TypeScript
Series
Advent of TypeScript Solutions
Episode 2 • Season 1

Day 2 | Christmas Cookie Inventory | Typehero Advent of TypeScript Challenge

465Reads
2 December, 2023

Explanation:

Santa needs help with keeping track of cookies in his workshop. Some elves, who are unhappy with their contracts, are using cookie inventory time to discuss their issues. Santa wants to make the inventory process faster to avoid these discussions.

Here, you need to update a type CookieSurveyInput so that it correctly represents the keys of a given object as a union of string literal types. The context is a playful scenario where Santa wants to speed up cookie inventory to minimize discussions among the elves.

TypeScript Concept Involved

The key TypeScript concept here is Mapped Types and Keyof Type Operator. Mapped types allow you to take an existing model and transform each of its properties into a new type. The keyof type operator is used to fetch the set of keys from a given type.

Solution

To solve this challenge, you need to define the CookieSurveyInput type such that it takes an object type as a parameter and returns a union type of its keys.

Here's how you can define CookieSurveyInput:

type CookieSurveyInput<T> = keyof T;
  • T is a generic type parameter that represents the object passed to CookieSurveyInput.
  • keyof T generates a union type of all the keys of the object T.

Link to Day one Challenge on TypeHero.dev:- https://typehero.dev/challenge/day-2

That's it! See you in the next Day 3 Challenge.