繁体   English   中英

TypeScript对于不匹配的返回值没有错误

[英]TypeScript no error on non-matching return value

为什么TypeScript不抱怨呢?

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
}

返回值, categories ,是类型的Array<ProductCategory> ,而不是一个无极,或甚至包装Result类。 那么,为什么让我犯这个错误呢? (有什么办法让它抱怨吗?)

提前致谢

返回值category是Array类型,不是Promise,

所有async函数都返回Promise 这是JavaScript规范的一部分。 如果返回常量,则本质上是Promise.resolve

当函数声明Promise<Result<Array<ProductCategory>>>返回类型时,您可以返回Result<Array<ProductCategory>>类型的值。

如果我有以下声明:

interface Result<T> {
    result: T;
}

我收到来自TypeScript编译器的错误:

Property 'result' is missing in type 'ProductCategory[]'.

您对Result的定义是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM