What is the best way to change the data types of all properties in Typescript?

Consider this scenario where an EmployeeDTO type is defined with properties like firstName, lastName, and dateOfBirth using Date

type EmployeeDTO = {
  firstName: string
  lastName: string;
  dateOfBirth: Date;
}

Now, we need a utility type that can transform all properties of type Date to be of type moment

type EmployeeDTO = {
  firstName: string
  lastName: string;
  dateOfBirth: moment;
}

Answer №2

Can this be adjusted to convert from a string[] to just a string?

In my testing, it seems that neither the extends Array<any> nor the

extends Record<string, any>
are handling this conversion correctly while still maintaining recursion.

I'm thinking about how to achieve this in a different way:

type HasStrArr = {
    strArr: string[];
}
type ModifyProp<T extends {}, TFrom, TTo> = {
    [K in keyof T]: Exclude<T[K], undefined> extends Array<any>
        ? ModifyProp<T[K], TFrom, TTo>
        : Exclude<T[K], undefined> extends Record<string, any>
        ? ModifyProp<T[K], TFrom, TTo>
        : Exclude<T[K], undefined> extends TFrom
        ? TTo
        : T[K];
};


type JustStrings = ModifyProp<HasStrArr,string[],string>

/* 
    {
        strArr: string[];
    }
 */

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to incorporate TypeScript type definitions into a JavaScript module without fully transitioning to TypeScript?

Although the title may be a bit confusing, it encapsulates my query in a succinct manner. So, here's what I'm aiming to achieve: I currently have an npm module written in JavaScript, not TypeScript. Some of the users of this module prefer using ...

Guidelines for Nestjs class-validator exception - implementing metadata information for @IsNotIn validator error handling

I have a NestJs data transfer object (dto) structured like this import { IsEmail, IsNotEmpty, IsNotIn } from 'class-validator'; import { AppService } from './app.service'; const restrictedNames = ['Name Inc', 'Acme Inc&ap ...

Spartacus has the capability to extend or override the PageType enum within the cms.model framework

I am facing a dilemma similar to the Spartacus situation. In brief, I am required to modify the PageType enum in cms.model by either overriding or extending it. The current enum consists of four values (content, product, category, catalog) and I must incl ...

Arrays causing Typescript compilation errors

There are a pair of typescript documents: one file as a module that implements the Client class export class Client { the other file as the main document that imports the module and generates an array of clients import c = module("client ...

Is there a way to modify a particular row in a table with Angular 5?

I am facing an issue with a table in my component that contains multiple records. Whenever I try to edit a specific row by clicking the edit button, it ends up making all rows editable instead. Here is how my table looks: The problem lies in the fact tha ...

Having trouble finding the "make:migration" command in Adonis 5 - any suggestions?

After reviewing the introductory documentation for Adonis Js5, I attempted to create a new API server. However, when compiling the code using "node ace serve --watch" or "node ace build --watch", I kept receiving an error stating "make:migration command no ...

Exploring the optimal procedures to asynchronously request an external API in Node.js using TypeScript

When handling a POST API request in a Node.js application built using TypeScript, it's necessary to make a call to an external API. This external API operates independently and must run in the background without impacting the response time. How can t ...

What is the significance of the IRenderFunction interface definition in FluentUI?

Recently diving into TypeScript, I've begun working with DetailsList in Fluent UI. Check it out here: https://developer.microsoft.com/en-us/fluentui#/controls/web/detailslist. I'm exploring the onRenderRow property, which is of type IRenderFunct ...

Harness the Power of Generics in TypeScript for Automatic Type Inference

function execute<T>(operation1: (input: T) => void, operation2: (input: { key: string }) => T): void {} execute((params) => {}, () => 23); // The params here can be correctly inferred as number execute((params) => {}, (arg) => 23) ...

Angular2's ngControl is unable to retrieve default values

I have been working on a form using Angular 2 (RC.3) and noticed that the `ngForm` directive is not recognizing default values set with the `value` attribute. // app.component.html <form (ngSubmit)="onSubmit(editForm.value)" #editForm="ngForm"> &l ...

Tips for generating a subject in rx.js while utilizing 2 parameters

Within my Angular2 application, I am utilizing a communication service to handle interactions between components. When calling the method: this.notification.create('test'); The service structure is as follows: export class NotificationServic ...

Enhancing the aesthetic appeal of Angular2 Google Maps

Hey there, I'm a newcomer to Angular2/Typescript and I've been working on styling a map in my Angular2 project. I integrated a map using the Angular2 Google Maps Components, but I'm having trouble with applying styles through the undocumente ...

Ensure that the bar width in react-chart-js-2 remains consistent regardless of the number of bars displayed

Currently, I am developing a React Chart JS component that displays a set of horizontal bars. My objective is to maintain the width of the bars at a consistent size regardless of the number of bars present (e.g., all bars at 30px). However, when additiona ...

Issue with sending props to TypeScript React component

Having a challenge with styling a simple button component using styled components. When trying to send a prop to the component, TypeScript throws an error saying "No overload matches this call". App.tsx import React from 'react'; import Button ...

Tips for implementing absolute import paths in a library project

In my workspace, I have a library with two projects: one for the library itself and another for a test application. ├── projects    ├── midi-app    └── midi-lib Within the workspace's tsconfig.json file, I set up paths for @a ...

Harness the power of Angular 2 on standard shared hosting services

Starting with AngularJS 2: Installed NodeJS Downloaded the initial project Ran it on Node Everything works perfectly! But now, how can I run it in a production environment on shared hosting (without Node and not on a VPS)? How can I open it in a browse ...

The type of a reference variable in a type definition of another variable

Can we reference the type of one variable (let's call it someVar) in TypeScript when declaring the type of another variable (anotherVar)? For example, instead of creating a separate type declaration for { complex: 'type' }, can we directly ...

Using aliases in npm packages is not supported

I am working on creating an npm package that I want to use in another application. During development, I set a path in tsconfig for importing various modules instead of using a relative path. However, when I download my package into the test app, it is una ...

Combining TypeScript into HTML resulted in an error: Uncaught ReferenceError clickbutton is not defined

Attempting to create a basic CRUD frontend without the use of any frameworks. I am encountering an issue when trying to include a TypeScript file (index.ts) in my index.html, as the functions called within it are showing as undefined. I understand that bro ...

Encountering an issue when attempting to import a non-source module from a node library while running a Typescript script

I have a script that takes input and utilizes the three.js library to apply geometric transformations to the data. I execute this script using ts-node pipeline.ts. Here is the structure of my project: ├── package-lock.json ├── package.json ├ ...