Mapping objects in Typescript to create a union of objects

I have been working on some TypeScript code and I seem to be having trouble getting it to work as expected. It would be greatly appreciated if someone could help me understand what I'm doing wrong or suggest a different approach.

Let's assume I have a Definition that consists of a function named name and data.

interface Definition<TName extends string, TData extends any> {
  name: TName;
  data: TData;
}

The Definition eventually transforms into an Action. The Action includes a function called method</code with <code>payload as the first parameter type.

interface Action<TDefinition extends Definition<string, any>> {
  name: TDefinition["name"];
  method: (data: TDefinition["data"]) => void;
}

It is essential for all elements of the Definition union to pass through the Changer.

interface Changer<TDefinition extends Definition<string, any>> {
  actions: Action<TDefinition>[];
}
...

What are your thoughts?

Answer №1

To tackle this issue, one can utilize distributive conditional types.

interface Changer<TDefinition extends Definition<string, any>> {
  actions: (
    TDefinition extends infer U extends Definition<string, any> 
      ? Action<U> 
      : never
  )[];
}

The objective is to transform the type of actions from

actions: Action<Definition<"one", 1> | Definition<"two", 2>>[]

to

actions: (Action<Definition<"one", 1>> | Action<Definition<"two", 2>>)[]

This transformation entails coercing the union elements of TDefinition to be distributed by a conditional into individual Actions.

Playground


Now the functions have the correct parameter types.

const set: Changer<Definition<"one", 1> | Definition<"two", 2>> = {
  actions: [
    {
      name: "one",
      method: (data) => data, // data: 1
    },
    {
      name: "two",
      method: (data) => data // data: 2
    }
  ],
};

However, attempting to execute this line will result in an error:

set.actions[0].method(1); // Argument of type 'number' is not assignable to parameter of type 'never'

The issue arises because the set variable lacks details about individual array elements due to its explicit type. To address this, consider creating the set using a generic function.

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

What is the significance of incorporating 'Actions' as data within the Redux framework?

According to Redux documentation, creating actions and action creators is necessary. Here's an example: function addTodo(filter) { return { type: SET_VISIBILITY_FILTER, filter } } Next step is to write reducers, like this: function t ...

Angular application experiencing issues with opening snackbar notifications

I'm currently working on a user registration application in Angular. My goal is to notify the user upon successful account creation or if an error occurs. I've been attempting to use a snackbar for this purpose, but it's not working as expec ...

What is the best way to implement multiple templates for a single component?

Is there a way to configure my Home Component based on the user's role? For instance: If the employee is an admin, then the home component should load the template URL designed for admins. Likewise, if the employee is a cashier, then the home compo ...

Creating an interface and setting a default value

I am exploring the use of interfaces in my models and want to establish a default value for them. export interface IPerson { id: string; name: string; } class Person implements IPerson { id = ''; name = 'John'; } export cla ...

The metadata for Company#companyTypesLinks could not be located. Please verify that the entity object has been correctly specified

I am facing an issue with the relationship between my entities. I have tried everything, but I still encounter the same problem. Here are my scripts: Company.entity.ts export class Company extends AppBaseEntity { @Column('text', { name: ' ...

Retrieving data from an API using VUEJS3 and Typescript

I am facing an issue with displaying data in my template. When I try to do so, the screen remains blank. I am using Vue.js 3 with TypeScript and I am fairly new to this technology. <template> <div> <img :src="datas[0].imag ...

Is it possible to import in TypeScript using only the declaration statement?

Is there a way to use a node module in TypeScript without explicitly importing it after compilation? For example: I have a global variable declared in a file named intellisense.ts where I have: import * as fs from 'fs'; Then in another file, ...

How to display a nested array in TypeScript following a specific structure

Can anyone assist with printing the variable below in the specified format? Data export const shop_items: Inventory:{ Toys{ Balls: { BallId: 001uy, BallName: Soccerball, SignedBy: David_Beckham, }, ...

Removing an attachment from the attachment object array nestled within a comment object array nested inside another object array

I am currently grappling with the challenge of removing an attachment from an array of attachments within a nested structure of comment objects. Despite several attempts, I have yet to find a solution that works effectively. export class CommentSection{ ...

tsc is not recognizing the configurations in my tsconfig.json file

Running tsc in my project's directory is causing an error to be outputted (as shown below). This is my first attempt at using TypeScript and Node.js. Please consider me as a complete beginner. Operating system: Ubuntu 15.10 64bits NPM version: 2.4. ...

The source files are expected to be contained within the 'rootDir' directory, which is not located at 'c:/Users/hasit/Desktop/typescript/src'

Can someone assist me with separating the Src folder and public folder in my project? When I try to do it in the tsconfig.json file, I encounter this error: "'rootDir' is expected to contain all source files." I have followed instructions from a ...

What is the process for discovering the kinds of models that can be generated with a Prisma client?

Ensuring data type correctness when creating a Prisma model named 'bid' is crucial. With auto-generated prisma types available, understanding the naming convention and selecting the appropriate type can be confusing. The bid schema looks like th ...

How can I retrieve the height of a dynamically generated div in Angular and pass it to a sibling component?

My setup consists of a single parent component and 2 child components structured as follows: Parent-component.html <child-component-1 [id]="id"></child-component-1> <child-component-2></child-component-2> The child-compo ...

Tips on injecting configuration into forRoot

Is there a method to inject configuration object into AppModule's forRoot() function? How can I access the configService if there is no constructor within the module? config.yml: smtp: host: 'smtp.host.com' port: 10 secure: true aut ...

Can you identify the specific function type passed through props?

interface IProps { handleCloseModal: React.MouseEventHandler<HTMLButtonElement> returnFunction: () => void; } export default function Modal({ children, returnFunction, handleCloseModal, }: React.PropsWithChildren<IProps>) { cons ...

Displaying messages in an Angular 2 chatbox from the bottom to the top

As a newcomer to using typescript, I am currently working on an angular 2 project and facing some challenges with creating a chatbox. My goal is to have new messages displayed at the bottom while pushing the old ones up one line at a time, as shown in this ...

Limit the typescript generic type to only a singular string literal value, preventing the use of unions

Within my project, I have introduced a generic entity type that utilizes a generic to determine a field type based on a specific set of string literals: type EntityTypes = 'foo' | 'bar' | 'baz'; type EntityMappings = { foo: ...

Modify the style of an element using a media query and Angular 4

Is there a way to update a class of an element based on the browser's width in my .ts file? matchMedia('(max-width: 400px)').addListener((mql => { if (mql.matches) { this.myclass = 'toggled'; } })); In the HTML, it shou ...

Adding a custom role in Angular TypeScript in Microsoft AppInsights is a straightforward process that can provide valuable

I have an angular project where I am looking to incorporate AppInsight with custom telemetry (role). The project is built in Angular using TypeScript, and I successfully integrated appinsights by following this tutorial. However, when attempting to add cus ...

TypeScript maintains the reference and preserves the equality of two objects

Retrieve the last element of an array, make changes to the object that received the value, but inadvertently modify the original last position as well, resulting in both objects being identical. const lunchVisit = plannedVisits[plannedVisits.length ...