cham
cham

Reputation: 10772

Typescript declaration for a promised string array

I have an async function that returns either a string or an array of strings. I've tried the following:

   async getAllAnnotationTimes(): Promise<string> | Promise<string[]> {
         return await this.app.client.getText(this.allAnnotationPositions);
   }

I have also used this declaration: Promise<string> | Promise<Array<string>>

Which gives this error: [ts] The return type of an async function or method must be the global Promise<T> type.

The error seems to related to the part after the or (Promise<Array<string>>)

How do I declare a promised string array?

Upvotes: 1

Views: 121

Answers (1)

zerkms
zerkms

Reputation: 255005

You would use

Promise<string | string[]>

which is literally a promise of a union type that is either a string or an array of strings.

Upvotes: 3

Related Questions