xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js & TS & VSCode error All In One

Node.js & TS & VSCode error All In One

Cannot redeclare block-scoped variable 'test'.ts bug

image

solutions

// 头部添加 export , 强制转换成 ES module;避免全局变量声明**重复**问题
export{};
// ...
// ...

export{};
// 尾部部添加 export , 强制转换成 ES module;避免全局变量声明**重复**问题

demos

  1. 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');

  1. 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

  1. export {} 模块化
// export {};

const func = () => {
  //
}

export {};

  1. 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://medium.com/@muravitskiy.mail/cannot-redeclare-block-scoped-variable-varname-how-to-fix-b1c3d9cc8206

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, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-05-21 23:01  xgqfrms  阅读(96)  评论(3编辑  收藏  举报