TypeScript: Typing Dynamic Objects and Subsets with Generics

TypeScript: Typing Dynamic Objects and Subsets with Generics

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.

How to use keyof, in, and extends keywords to type dynamic functions in TypeScript.

TypeScript gained popularity over 2020 and is now in the  upper bracket of the programming salary range. If you are a JavaScript developer you have most likely been exposed to TypeScript in some project you have worked on.

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:

  • *Basic: *How to use primitive types with a pick function — 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.
  • Intermediate: TypeScript keyword keyof will be explored with generics to enhance our types to coincide with the object properties to be returned by our function.
  • Advanced: We’ll delve deeper by enhancing the pick return type to only include properties being picked out from the object in question. We will be using the extends and in keywords to achieve this.

typescript development javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

TypeScript Tutorial For JavaScript Developers - TypeScript Basics

TypeScript Tutorial For JavaScript Developers - TypeScript Basics. I will show you guys 4 example of JavaScript code, and how to convert it to TypeScript. This is a typescript beginners tutorial.

What, Why, and How of Typescript for JavaScript Developers

What, Why, and How of Typescript for JavaScript Developers. If you are a Javascript developer, you must have heard about Typescript at one point or another. What is Typescript, what is its type system, and how would it benefit you as a Javascript developer to use Typescript in your next project?

What is TypeScript? Why TypeScript? and Why Not TypeScript?

TypeScript extends JavaScript by adding Types. There are many great reasons to switch to TypeScript. Especially if your team uses JavaScript. There are some reasons to not use TypeScript as there are with any language or framework.

How long does it take to develop/build an app?

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.