本文介绍了TypeScript意外的标记,构造函数,方法,访问器或属性是预期的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用typescript在类中编写函数。
Just trying to write a function within a class using typescript.
class Test
{
function add(x: number, y: number): number {
return x + y;
}
}
这会导致以下错误:
我复制了以下示例:
我错过了什么吗?我很困惑!
Am I missing something? I'm confused!
推荐答案
你不应该使用函数
Typescript类定义中的关键字。请改为尝试:
You shouldn't use the function
keyword in a Typescript class definition. Try this instead:
class Test {
add(x: number, y: number): number {
return x + y;
}
}
这篇关于TypeScript意外的标记,构造函数,方法,访问器或属性是预期的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!