Function return type inference is limited (arkts-no-implicit-return-types) <ArkTSCheck>Operator ‘+‘

Function return type inference is limited (arkts-no-implicit-return-types) <ArkTSCheck>
Operator '+' cannot be applied to types 'Food' and 'number'. <ArkTSCheck>

const total = foods.reduce((a, b) => {
  return a + b.price * b.count
},)

第一个错误 Function return type inference is limited (arkts-no-implicit-return-types) <ArkTSCheck> 提示您函数的返回类型应该明确声明,而不是依赖于隐式推断。

第二个错误 Operator '+' cannot be applied to types 'Food' and 'number'. <ArkTSCheck> 表示您正在尝试将一个 Food 类型的对象和一个数字进行相加操作,这在类型上是不兼容的。

  1. reduce 方法的回调函数需要返回一个累加值。在您的代码中,没有返回初始值 0 ,导致语法错误。
  2. 您不能直接将一个对象(如 a )和一个数字( b.price * b.count )相加,需要先提取出价格进行累加。

 

const total = foods.reduce((a, b) => {
  return a + b.price * b.count
}, 0)

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params) 是指在 TypeScript 中,编译器在进行类型推断时对于泛型函数的调用有一定的限制。这意味着有时候 TypeScript 编译器无法自动推断出在调用泛型函数时应该使用的具体类型参数,因此需要开发者明确指定这些类型参数。 例如,在 TypeScript 中,当你定义一个泛型函数时,通常不需要在调用时指定类型参数,因为编译器能够根据函数调用时提供的参数或者上下文来推断出正确的类型。但是在某些情况下,由于类型推断的限制,编译器可能无法正确推断类型参数,这时就需要开发者手动指定它们。 为了解决这个问题,你可以直接在调用函数时提供类型参数,明确指出期望的具体类型。这样做可以提高代码的类型安全性,并且帮助编译器正确地执行类型检查。 例如,假设有一个泛型函数 `identity<T>(arg: T): T`,它的目的是返回传入的参数,TypeScript 通常能够根据传入的参数自动推断出类型参数: ```typescript let output = identity("myString"); // TypeScript 推断出 output 的类型为 string ``` 但在某些情况下,如下: ```typescript let output = identity(someOtherVariable); // 如果 someOtherVariable 类型不确定,TypeScript 可能无法推断类型 ``` 在这个例子中,由于 `someOtherVariable` 的类型未知,编译器可能无法推断出调用 `identity` 时的类型参数。这时,你需要明确指定类型参数: ```typescript let output = identity<string>(someOtherVariable); // 明确指定类型参数为 string ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值