Node.js & TS & VSCode error All In One
Node.js & TS & VSCode error All In One
Cannot redeclare block-scoped variable 'test'.ts bug
solutions
// 头部添加 export , 强制转换成 ES module;避免全局变量声明**重复**问题
export{};
// ...
// ...
export{};
// 尾部部添加 export , 强制转换成 ES module;避免全局变量声明**重复**问题
demos
- global scope
// TS throw duplicate declare `test` bug ❌
const test = (msg: string) => {
console.log(msg);
};
test('hello TS');
var test = function (msg) {
console.log(msg);
};
test('hello TS');
- module scoped
// fix TS throw duplicate declare `test` bug ✅
export {};
const test = (msg: string) => {
console.log(msg);
};
test('hello TS');
"use strict";
exports.__esModule = true;
var test = function (msg) {
console.log(msg);
};
test('hello TS');
typescript error & cannot redeclare block-scoped variable
Cannot redeclare block-scoped variable 'func'.ts(2451)
a.ts
const func = () => {
//
}
b.ts
const func = () => {
//
}
solutions
export {}
模块化
// export {};
const func = () => {
//
}
export {};
namespace
命名空间
namespace a {
const func = () => {
//
}
}
ts-node
ts-node is a TypeScript execution engine and REPL for Node.js.
It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node.js without precompiling.
ts-node 是一个 TypeScript 执行引擎
和 Node.js 的 REPL
。
它使用 JIT
将 TypeScript 转换为 JavaScript,使您无需预编译
即可直接在 Node.js 上执行 TypeScript。
$ npm i -D ts-node
$ ts-node ./app.ts
# 等价于
$ tsc ./app.ts & node ./app.js
shell script & env
#!/usr/bin/env ts-node
console.log("Hello, world!")
https://typestrong.org/ts-node/docs/
https://typestrong.org/ts-node/
https://www.npmjs.com/package/ts-node
https://github.com/TypeStrong/ts-node
refs
https://www.cnblogs.com/Jamie1032797633/p/11131835.html
https://stackoverflow.com/questions/35758584/cannot-redeclare-block-scoped-variable-typescript
https://github.com/microsoft/vscode/issues/22436
https://www.jianshu.com/p/78268bd9af0a
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14797698.html
未经授权禁止转载,违者必究!