Smart Typescript Bulk Property Assignment

Smart Typescript Bulk Property Assignment

I want the objects I use in my TypeScript code to let me set multiple properties on them in one go. My objects must have a method setProps such that when given a properly typed map of properties, the method sets the corresponding properties on the objects.

The Aim

I want the objects I use in my TypeScript code to let me set multiple properties on them in one go. My objects must have a method setPropssuch that when given a properly typed map of properties, the method sets the corresponding properties on the objects.

The Approach

I have a base class, say BaseObject, that has the publicsetProps method. setProps must take parameters that are compatible with the instance of a class derived from BaseClass. This is achieved with generics. The method will look like the following piece of code:

I constrain the type parameter T to represent a type that inherits from BaseObject by using the syntax <T extends BaseObject>.

Now, I want to assign the properties of the passed parameter to the object on which setProps was called. To achieve this, I proceed as shown in the following excerpt:

The for in loop gets all the keys in values and for each such property, if its value is not null, assigns it to the same property on the calling object.

generics typescript mapped-types

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

TypeScript Generic Types and How to Properly Implement Them

In TypeScript, Generic Types enable us to define a placeholder type for a variable we do not know enough yet to precise its type. Consider the following snippet:

Nominal typing in Typescript

Type systems are typically categorized as either structural or nominal. Languages like Java and Scala have primarily nominal type systems, whereas a language like Typescript has a structural type system. Let’s take a brief look at both systems.

Types, Type Systems and TypeScript

A type system is a set of rules for performing various consistency and correctness checks in a program. There are various definitions and classifications for type systems

Type Inference in TypeScript • Full-Stack TypeScript with Kretes • E05

Type inference is the automatic detection of the data type of an expression in a programming language. This feature is present in some strongly statically typed languages, including TypeScript.

Generics & Abstract Classes in TypeScript

In the above case the input and output are specified as type any which defeats the whole purpose of TypeScript as it will never find a type error if anything is allowed. Instead we could use generic syntax as denoted by <> to pass in a generic type when the function is called.