只是尝试使用类型记录在类中编写一个函数。
class Test
{
function add(x: number, y: number): number {
return x + y;
}
}
这将导致以下错误:
TypeScript意外令牌、构造函数、方法、访问器或属性。
我从:https://www.typescriptlang.org/docs/handbook/functions.html复制了这个示例
我是不是遗漏了什么?我很困惑!
发布于 2017-03-28 13:21:59
您不应该在类型记录类定义中使用function
关键字。试一试:
class Test {
add(x: number, y: number): number {
return x + y;
}
}
发布于 2017-03-28 13:26:02
TypeScript不允许function
声明作为类成员;它的语法略有不同.
class Test
{
// This will bind the add method to Test.prototype
add(x: number, y: number): number
{
return x + y;
}
// This will create a closure based method within the Test class
add2 = (x: number, y: number) => {
return x + y;
}
}
https://stackoverflow.com/questions/43070702
复制相似问题