TypeScript: Typing dynamic objects and subsets with generics. Generic types popularity over 2020 and is now in the upper bracket of the programming salary range. Being able to define generic types that successfully type objects without compromises is key when working with API responses.
This article, along with others surrounding it, will introduce the reader to advanced TypeScript concepts by explaining them from the ground up with a typical use case — working with objects and subsets of those objects in a dynamic way such that we do not know the exact structure of these objects, but want to define them without compromise.
Being able to define generic types that successfully type objects without compromises is key when working with API responses and event driven applications, whereby the data you receive is filtered and extracted into various components for various functions to handle.
This article focuses on this use case. Instead of working in live environments, we will keep things straightforward by working with simple functions designed to extract properties from objects and return a smaller object of those properties.
The article will gradually build up the complexity of our types with 3 sections:
pickfunction — a function designed to return a subset of an object’s properties that correspond with the keys provided. The issues with such basic typing will be presented.
keyofwill be explored with generics to enhance our types to coincide with the object properties to be returned by our function.
pickreturn type to only include properties being picked out from the object in question. We will be using the
inkeywords to achieve this.
This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.