打字稿lambda错误(Typescript lambda error)

编程入门 行业动态 更新时间:2024-10-28 14:37:21
打字稿lambda错误(Typescript lambda error)

我正在观看打字稿教程,有一点我必须编写这行代码

var squareItSimpler = function(h:number, w:number) => h * w;

但我无法让它发挥作用。 我一直在收到错误

The command "C:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc.exe" --module AMD --target ES3....exited with code 1

而我只是不知道该怎么做,我在这里做错了什么?

有趣的是,这个休闲工作:

var squareItSimplest = (h:number, w:number) => h * w;

我正在使用TypeScript 0.9.0.1

I'm watching a typescript tutorial and at one point i have to write this line of code

var squareItSimpler = function(h:number, w:number) => h * w;

But i just can't get it to work. I keep getting the error

The command "C:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc.exe" --module AMD --target ES3....exited with code 1

And i just don't know what to do, am i doing something wrong here ?

The funny this is that the fallowing works:

var squareItSimplest = (h:number, w:number) => h * w;

I'm using TypeScript 0.9.0.1

最满意答案

当您在Typescript中执行类似lambdas的操作时,请不要使用function关键字。 也许你的教程有错误。

编辑 - 从规范 :

TypeScript支持箭头函数表达式,这是ECMAScript 6计划的新功能.Arow函数表达式是函数表达式的一种紧凑形式,它省略了function关键字并对此进行了词法作用域。

When you do lambdas like that in Typescript, you leave off the function keyword. Perhaps your tutorial has an error.

edit — from the spec:

TypeScript supports arrow function expressions, a new feature planned for ECMAScript 6. Arrow function expressions are a compact form of function expressions that omit the function keyword and have lexical scoping of this.

更多推荐

本文发布于:2023-07-20 11:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198658.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   lambda   error   Typescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!