you*_*ziz 6 interface typescript
我是 Typescript 的新手,我仍在寻找出路,我到处搜索这个问题,否则我不会问这个问题。我在接口名称处出现语法错误,不知道如何解决。这是我的简短代码:
interface Cars {
name:string
model:string
topSpeed:number
colors:string[]
speedPrint(carSpeed:number):void
}
class BMW implements Cars{
name = 'BMW X6'
model = 'S'
topSpeed = 320
colors = ['cobalt red','phantom blue','white']
speedPrint(topSpeed:number):void{
console.log(`My car top speed is ${topSpeed}`)
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
app.ts:1
interface Cars {
^^^^
SyntaxError: Unexpected identifier
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
Run Code Online (Sandbox Code Playgroud)
您的代码中没有 Typescript 语法错误。你可以看到它在 Typescript Playground 中完全没有错误。
但从堆栈跟踪来看,您似乎正在尝试直接从 Node.js 执行 Typescript,就像它是 Javascript 一样。这可以解释这个错误,因为 Javascript 不支持接口;这是 Typescript 的事情。您需要使用tsc,esbuild或等工具将 *.ts 文件编译为 *.js deno,或者您甚至可以使用 直接运行 Typescript ts-node。
如果您不提供有关您的设置、您执行的产生该错误的命令等的更多信息,我无法给您更具体的答案。
小智 7
我遇到了同样的错误,但我执行了以下操作并且它有效:
npm install -g typescripttsc filename.tsnode ./file.js(不是 ts)看看终端上会发生什么