【问题标题】:Interface Unexpected identifier接口意外标识符
【发布时间】:2021-07-07 13:27:54
【问题描述】:

我有一个名为 main.ts 的文件,其中包含以下代码 sn-p :

interface bodyInfos {
    height?: any;
    mass?: any;
}

const calculateBmi = (user: bodyInfos) => {
    return user.mass / ( user.height ** 2 );
};

let foo: bodyInfos;
let bar: bodyInfos;

// Foo
foo.height = 172;
foo.mass = 75;
// Bar
bar.height = 180;
bar.mass = 90;

console.log('foo BMI', calculateBmi(foo) );
console.log('bar BMI', calculateBmi(bar) );

问题是每当我尝试使用以下命令执行文件 main.ts 时:node main.ts 我在控制台上收到错误消息:

interface bodyInfos {
          ^^^^^^^^
SyntaxError: Unexpected identifier

我需要你的支持。

【问题讨论】:

标签: javascript typescript interface


【解决方案1】:

找到了,谢谢@VLAZ的澄清,

我所做的是,我需要使用 tsc main.ts 将 main.ts 编译成 main.js 并在新编译的文件上运行命令 node main.js,瞧,它完美运行。

这里稍微解释一下,实际上我尝试使用ts-node编译文件但没有正常工作,控制台输出错误太多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2017-09-05
    • 2020-07-10
    • 2012-06-19
    • 2014-04-10
    • 2013-10-14
    相关资源
    最近更新 更多