'The instantiation of 'TSearchInput' may involve a type that is completely different from that of 'RotatingItemSearchInput'

I am encountering an issue where I want each record in the object to have different value types, but I keep running into these errors:

1 - 'TSearchInput' could potentially be instantiated with a type that is not related to 'RotatingItemSearchInput'

2 - 'K' could potentially be instantiated with a type that is not related to 'number'

To illustrate the problem, I have created this basic demo:

class RotatingItemSearchInput {}
class OtherItemSearchInput {}

const rotatingItemService = (input: RotatingItemSearchInput) =>
  new Promise((res: (val: number) => void) => res(1));

const otherItemService = (input: OtherItemSearchInput) =>
  new Promise((res: (val: string) => void) => res('result'));

const filtersMap: Record<
  string,
  <TSearchInput, K>() => {
    searchInput: TSearchInput;
    search: (searchInput: TSearchInput) => Promise<K>;
  }
> = {
  rotatingItem: () => ({
    searchInput: new RotatingItemSearchInput(),
    search: (searchInput) => rotatingItemService(searchInput),
  }),
  otherItem: () => ({
    searchInput: new OtherItemSearchInput(),
    search: (searchInput) => otherItemService(searchInput),
  }),
};

I would greatly appreciate your assistance in resolving this issue.

Playground

Answer №1

Observe this

definition Obj<T, K> {
  [key: string]: {
    userInput: T;
    query: (val: T) => Promise<K>;
  };
}

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

I am unable to utilize autocomplete with types that are automatically generated by Prisma

Currently, I am working on a project utilizing Next and Prisma. Within my schema.prisma file, there are various models defined, such as the one shown below: model Barbershop { id String @id @default(uuid()) name String address String ...

refresh my graph on the user interface once the service responds with JSON data

After obtaining the object successfully from my API, I have already displayed my custom graph component with default values. However, I need to update the 'chart1Title' of the graph component with the value of 'title' from the object. ...

The parameter "disabled=false" is not functioning properly in TypeScript 2.1

Struggling to deactivate a field by accessing the element ID in TypeScript 2.1. Came across this syntax for TypeScript 1.5, but unfortunately, it doesn't seem to work in 2.1. Any assistance would be greatly appreciated. ( document.getElementById(&apo ...

What is the best way to showcase a file edited in Emacs within Atom?

The coding project I'm working on is built with Typescript, but I don't believe that's relevant. I've noticed that Emacs has a unique approach to indentation. According to the documentation, in Text mode and similar major modes, the TAB ...

Break down and extract elements using typedEvent in TypeScript

Within the external library, there is the following structure: export interface Event extends Log { args?: Result; } export interface TypedEvent<EventArgs extends Result> extends Event { args: EventArgs; } export type InstallationPreparedEven ...

Troubleshooting: Angular add/edit form issue with retrieving data from a Span element within an ngFor loop

I am currently working on an add/edit screen that requires submitting a list, among other data. The user will need to check 2-3 checkboxes for this specific data, and the saved record will have multiple options mapped. Here is what the HTML looks like: &l ...

Create an array variable for services in Angular

My goal is to define this user as an array. users = new BehaviorSubject<any>([]); In my component, I am attempting to add the userID to the array. this.Service.users.push(userID); To subscribe to it, I use the following code: this.Service.users.su ...

When using TypeScript and React with Babel, an error may occur: "ReferenceError: The variable 'regeneratorRuntime'

I've been delving into learning typescript, react, and babel, and here is the code I've come up with: export default function App(): JSX.Element { console.log('+++++ came inside App +++++++ '); const {state, dispatch} = useCont ...

Utilize Typescript to Invoke Functions of Different Components in Angular 2

Hello everyone, I am a newcomer to Angular 2 and I'm looking to utilize the value of one component in another component. This will help me populate data based on that particular value. In my setup, I have three Components - App.Component, Category.Co ...

What is the best way to modify the KeyName in an object?

Having difficulty parsing an Object by changing keynames due to the error message "Element implicitly has an 'any' type because expression of type 'keyof SignInStore' can't be used to index type '{}'". interface SignInSto ...

Ways to invoke a slice reducer from a library rather than a React component

I've been working on a React application using the React Boilerplate CRA Template as my foundation. This boilerplate utilizes Redux with slices, which is great for updating state within a React component. However, I'm facing a challenge when try ...

Launching Nest.js application from Visual Studio Code

Currently experimenting with a new framework called which seems promising as it integrates TypeScript into Node, similar to Angular. I'm using the starter template from https://github.com/kamilmysliwiec/nest-typescript-starter. I can start it withou ...

React and Typescript: Executing a function on one element by clicking on another element

I've been working on a fun mini-game where two adorable hamster pictures battle it out to determine the cutest one. After each round, when the user clicks on the winning picture, I need to update my database with the number of wins and defeats for ea ...

Drawing a real-time curve using Phaser 3

After reading the article at the following link, I am attempting to create a dynamic curve showing where a bullet intersects with the land in my game before firing. Any suggestions or ideas on how to achieve this would be greatly appreciated. Thank you. L ...

Using parameters and data type in Typescript

When I remove <IFirst extends {}, ISecond extends {}> from the declaration of this function, the compiler generates an error. Isn't the return value supposed to be the type after the double dot? What does <IFirst extends {}, ISecond extends { ...

Using Rxjs to reset an observable with combineLatest

My scenario involves 4 different observables being combined using "combineLatest". I am looking for a way to reset the value of observable 2 if observables 1, 3, or 4 emit a value. Is this possible? Thank you! Mat-table sort change event (Sort class) Mat- ...

Utilize the power of relative import by including the complete filename

When working on my TypeScript project, I have noticed that to import ./foo/index.ts, there are four different ways to do it: import Foo from './foo' // ❌ import Foo from './foo/index' // ❌ import Foo from './foo/i ...

`How can you effectively simulate class modules in Jest?`

Working with the amplitude module requires me to start by creating an instance of a class and then use its methods. Here's the initial code snippet: var Amplitude = require('amplitude'); const amplitude = new Amplitude(process.env.amplitude ...

Why does TypeScript trigger an ESLint error when using `extend` on a template string?

I am looking to create a TrimStart type in the following way: type TrimStart<T extends string> = T extends ` ${infer Rest}` ? TrimStart<Rest> : T; type TT = TrimStart<' Vue React Angular'>; // 'Vue React Angular' H ...

Encountering a type error when attempting to filter TypeORM 0.3.5 by an enum column that is

I have the following configuration: export enum TestEnum { A = 'A', B = 'B' C = 'C' } @Entity() export class User { @PrimaryGeneratedColumn() id: number @Column({enum: TestEnum}) test: TestEnum } ...