1

I have a question in regards to extending an interface to allow duplicate fields. Basically I need to create 2 requests. One request handles one event, the other request handles 2 events.

Example:

One event request:

events[
{
  "name": "EventOne"
  "description": EventOne Description
}
]

Two events request:

events[
{
  "name": "EventOne"
  "description": EventOne Description
},
{
  "name": "EventTwo"
  "description": EventTwo Description
}
]

So I have a method then sends a request like so:

export function performRequest(params: IEvent) {
  return request(test)
    .post(`/events`)
    .send(params);

I then have two interfaces. One deals with a single request object (IEventDetails), the other extends the IEventDetails and handles the two events request which is known as IMultiEventDetails:

interface IEvent {
  events: [IEventDetails];
}

interface IEventDetails {
name?: string;
description?: string;
}

interface IMultiEventDetails extends IEventDetails{
name?: string;
description?: string;
}

Now when I call on my requests, it works for the single event, but not for the multi event as it gives me the error:

Source has 2 element(s) but target allows only 1

When I call my requests:

Below works:

const response = await performRequest({
events[{
      name: "EventOne"
      description: "EventOne Description"
}]
...

Below errors:

    const response = await performRequest({
    events[{
          name: "EventOne"
          description: EventOne Description
    },
    {
          name: "EventTwo"
          description: EventTwo Description
    }]
    ...

This makes sense as I am not calling on the IMultiEventDetails. I am trying to avoid creating one method to call on single event and another method to call on multi event. So it there a way in order to call on IEvent and have it to be able to handle single or multi event requests?

2
  • IEventDetails is exactly the same as IMultiEventDetails...? Commented Apr 27, 2022 at 15:05
  • Not exactly the same, I multiEventDetails request has name and description appear twice, just like the last snippet I posted in my question Commented Apr 27, 2022 at 15:11

1 Answer 1

2

[IEventDetails] specifies an array with one element

IEventDetails[] specifies an array of IEventDetails type, so any number of IEventDetails is fine

interface IEvent {
  events: IEventDetails[];
}

interface IEventDetails {
  name?: string;
  description?: string;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that worked. Ok I learned something new and that was my mistake, placing it as [IEventDetails] as I thought as events had square brackets to surround it's inner objects, I did it that way. Cool, thanks for your explanation and answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.