TypeScript意外的标记,构造函数,方法,访问器或属性是预期的 [英] TypeScript Unexpected token, A constructor, method, accessor or property was expected

查看:1862
本文介绍了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;
    }
}

这会导致以下错误:


TypeScript意外的令牌,构造函数,方法,访问器或
属性是预期的。

TypeScript Unexpected token, A constructor, method, accessor or property was expected.

我复制了以下示例: https:// www .typescriptlang.org / docs / handbook / functions.html

我错过了什么吗?我很困惑!

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意外的标记,构造函数,方法,访问器或属性是预期的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆