Hon*_*iao 1 typescript lodash typescript2.0
我正在使用TypeScript 2.x和lodash.
我用的时候
console.log(_.reduce([1, 2], (result, n) => result + n)); // 3
Run Code Online (Sandbox Code Playgroud)
它给了我错误:
错误TS2365:运算符"+"无法应用于类型"{}"和"数字".
改为result: number将解决问题.
console.log(_.reduce([1, 2], (result: number, n) => result + n)); // 3
Run Code Online (Sandbox Code Playgroud)
TypeScript只能知道n基于的数字[1, 2].但它不知道result它返回之前的类型.所以你需要明确告诉它.
例如,
console.log(_.reduce([1, 2], (result, n) => 'hi')); // 'hi'
Run Code Online (Sandbox Code Playgroud)
result字符串的类型而不是数字.
| 归档时间: |
|
| 查看次数: |
1531 次 |
| 最近记录: |