我试图用类型对变量进行注释,但是在运行代码时只会发出一个错误:
let foo: number = 23;
console.log( foo );
// let foo: number = 23;
^
// SyntaxError: Unexpected token ':'
// [90m at Object.compileFunction (node:vm:352:18)[39m
// [90m at wrapSafe (node:internal/modules/cjs/loader:1031:15)[39m
// [90m at Module._compile (node:internal/modules/cjs/loader:1065:27)[39m
// [90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)[39m
// [90m at Module.load (node:internal/modules/cjs/loader:981:32)[39m
// [90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)[39m
// [90m at Function.executeUserEntryPoint [as runMain] // (node:internal/modules/run_main:79:12)[39m
// [90m at node:internal/main/run_main_module:17:47[39m
发布于 2021-12-07 01:31:25
你有两个选择:
首先使用tsc myfile.ts
将其编译为普通JS,然后使用node myfile.js
运行JS文件。
或者npm install ts-node
,它允许您像运行JS文件那样运行TS文件:ts-node myfile.ts
https://stackoverflow.com/questions/70257602
复制相似问题