我試圖用型別注釋我的變數,但是在運行代碼時它只是發出一個錯誤:
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
uj5u.com熱心網友回復:
您有兩個選擇:
首先將其編譯為純 JStsc myfile.ts并使用 .js 運行 JS 檔案node myfile.js。
或者npm install ts-node讓你像運行 JS 檔案一樣運行 TS 檔案:ts-node myfile.ts
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/375618.html
