Reputation: 175
node 14.7.0, npm 6.14.7, typescript 3.7.3.
I have a method that queries a postgres database and, depending on a param, will return the first row it finds or the entire result from the query. Similar to this:
async getRecord(options: {singleRow: boolean}): Promise<QueryResult> | Promise<Record> {
const result:QueryResult = await postgres.query(...);
if (options && options.singlerow) {
return result.rows[0];
}
return result;
}
However, I receive the following error: The return type of an async function or method must be the global Promise<T> type.
I do not receive an error when I remove the | Promise<Record>
section, even though the first return (in the if statement) returns a Promise<Record>
. What should I do to fix this?
Upvotes: 0
Views: 504
Reputation: 3227
Return Promise<QueryResult | Record>
async
functions need a Promise to box whatever type they return, there's just one top level Promise-type-generic that wraps the return type.
Upvotes: 3