【发布时间】:2020-11-13 14:50:52
【问题描述】:
我正在尝试对界面进行简单的测试。 代码如下:
interface TestInterface {
id: number;
text: string;
}
const testInterfaceImplementation: TestInterface = {
id: 1,
text: 'sample text'
};
console.log(testInterfaceImplementation.text);
当我使用 Node.js 配置运行此代码时,我收到此错误:
interface TestInterface {
^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
当我在没有这个接口的情况下运行代码时,它可以正常工作:
const testInterfaceImplementation = {
id: 1,
text: 'sample text'
};
console.log(testInterfaceImplementation.text);
有什么问题?我也尝试将界面移动到不同的 .ts 文件,但仍然出现错误。
tsconfig.json 文件:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
【问题讨论】:
标签: javascript typescript intellij-idea syntax interface