Reputation: 3933
Why doesn't TypeScript complain about this?
async GetCategoriesBySet(set: Set): Promise<Result<Array<ProductCategory>>> {
let categories: Array<ProductCategory> = []
if (!set.theme || !set.subtheme || !set.title) {
return Promise.resolve(new Result<Array<ProductCategory>>({res: null, err: "Set data is not valid"}))
}
categories.push(await this.GetCategory(set.theme, {parent: (await this.GetCategory('Themes')).id}))
return categories
}
The return value, categories
, is of type Array<ProductCategory>
, not a Promise, or even the wrapping Result
class. So why is it happy to let me make this mistake? (And is there any way to get it to complain?)
Thanks in advance
Upvotes: 0
Views: 151
Reputation: 1613
When your function declares Promise<Result<Array<ProductCategory>>>
return type you can return value of type Result<Array<ProductCategory>>
.
If I have following declaration:
interface Result<T> {
result: T;
}
I receive error from TypeScript compiler:
Property 'result' is missing in type 'ProductCategory[]'.
What is your definition of Result
?
Upvotes: 0
Reputation: 276239
The return value, categories, is of type Array, not a Promise,
All async
functions return a Promise
. This is a part of the JavaScript spec. If you return a constant it is essentially Promise.resolve
ed.
Upvotes: 3