Class 类型脚本:SyntaxError:意外标记

Class 类型脚本:SyntaxError:意外标记,class,typescript,runtime-error,unexpected-token,Class,Typescript,Runtime Error,Unexpected Token,我正在尝试实现这个简单的typescript代码。但在运行node命令时出现了奇怪的错误。有人能帮忙吗 梅因酒店 class Wedding { bond:string; money:number; isWeddingPossible() { if(this.bond === 'Strong' && this.money > 10000 ){ console.log('Wedding can be don

我正在尝试实现这个简单的typescript代码。但在运行node命令时出现了奇怪的错误。有人能帮忙吗

梅因酒店

class Wedding {

    bond:string;
    money:number;

    isWeddingPossible() {
        if(this.bond === 'Strong' && this.money > 10000  ){
            console.log('Wedding can be done.');
        }
        else{
            console.log("Go to hell");
        }
    }
}

let wedding = new Wedding(); 
wedding.bond = 'Not Strong'; 
wedding.money = 500; 
wedding.isWeddingPossible();
运行命令:tsc main.ts&&node main.ts

错误:

/ts-hello/main.ts:3
    bond:string;
        ^

SyntaxError: Unexpected token :
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

您应该运行
node main.js
,而不是
main.ts
。您编译
.ts
文件,它将创建
.js
文件。因此,您需要通过node运行编译的
.js
文件。

您应该运行
node main.js
,而不是
main.ts
。您编译
.ts
文件,它将创建
.js
文件。因此,您需要通过node运行编译后的
.js
文件。

如果要立即运行main.ts,必须使用npm包ts node,例如“ts node main.ts”。如果要立即运行main.ts,必须使用npm包ts node,例如“ts node main.ts”