1

I have the next piece of code:

public async insert(data: iFlower | iFlower[]): Promise<iFlower> | Promise<iFlower[]> {
 await this.insert(data);
}

private async insert(data: iFlower): Promise<iFlower>{
 ....
 return data;
}

private async insert(data: iFlower[]): Promise<iFlower[]> {
 ....
 return data;
}

iFlower is:

export interface iFlower {
   color: string;
   number: string;
}

I get the following errors: The return type of an async function or method must be the global Promise<T> type. Duplicate function implementation. 'insert' is declared but its value is never read.

Is it because iFlower is an interface?

1 Answer 1

1

When you do an overload in typescript, the only thing you provide multiples of is the types. The actual implementation is just a single function. So, you'll do a series of types for the function, then one implementation which is compatible with all those types, as in:

private async insert(data: iFlower): Promise<iFlower>;
private async insert(data: iFlower[]): Promise<iFlower[]>;
private async insert(data: iFower | iFlower[]): Promise<iFlower> | Promise<iFlower[]> {
  // Your code here. Maybe something like:
  if (Array.isArray(data)) {
    ... 
    return data;
  } else {
    ... 
    return data;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.