0

Currently I have this interfaces:

export interface requestPayload {
  agentId: string;
}

export interface Agent {
  id: string;
  name: string;
  requestPayload: requestPayload;
}

My problem is requestPayload property of Agent interface is now an object with { agentId: string; }

But requestPayload should be anything, like an array, number, string, and maybe that object that I defined with requestPayload interface.

I got a suggestion to "use a generic", how can I change that to a generic? I'm not sure how to proceed.

2
  • Why make a requestPayload interface in the first place if requestPayload doesn't have to match the type? Commented Dec 12, 2020 at 19:02
  • At first I thought that requestPayload would be always an object, but now it seems that can be anything, so how can define it's type? Commented Dec 12, 2020 at 19:04

2 Answers 2

1

Using generics is quite right solution for your case:

export interface Agent<T> {
  id: string;
  name: string;
  requestPayload: T;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I was posting that as a solution because I wasn't sure if it was correct
Depends on your tasks and goals. If you wish to shape every instance of Agent that this solution is for you
0

You can use unknown:

export interface Agent {
  id: string;
  name: string;
  requestPayload: unknown;
}

As the docs indicate, it was meant for this sort of thing.

2 Comments

That's a solution but I can't, the Eslint config for this project doesn't allow any types.
Ok, with unknown I have other eslint problems, when I want to acces a propery of an object it says that it's not possible on unknown type

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.