typescript 不能重新声明块范围的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38636395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:41:40  来源:igfitidea点击:

Cannot redeclare block-scoped variable

typescripttypescript1.8

提问by ken

I have node program which ultimately uses commonjs and therefore my JS files start with a number of requirestatements. I renamed these JS files to TS with the hope that I could incrementally move into Typescript but am getting these errors:

我有最终使用 commonjs 的节点程序,因此我的 JS 文件以许多require语句开头。我将这些 JS 文件重命名为 TS,希望我可以逐步迁移到 Typescript,但出现以下错误:

enter image description here

在此处输入图片说明

from the following code:

从以下代码:

const RSVP = require('rsvp');
const moment = require('moment');
const firebase = require('universal-firebase');
const email = require('universal-sendgrid');
const sms = require('universal-twilio');
const merge = require('merge');
const typeOf = require('type-of');
const promising = require('promising-help');
const _ = require('lodash');

const up = require('./upload');
const audience = require('./audiences');
const message = require('./messages');

The locally referenced modules like upload, audiences, and messagesarelikely to define most (all?) of the same modules such lodash, etc. I'm guessing that somehow the namespace scope is not being respected between modules but I'm not sure why.

在本地引用模块喜欢uploadaudiencesmessages可能大部分(全部?)这种相同的模块定义lodash,等我猜不知何故命名空间范围不被尊重模块之间,但我不知道为什么。

I'm also unclear whether using ES6 importsyntax would properly transpile to a ES5 commonjs"require" module format (this is using Node 0.10.x).

我也不清楚使用 ES6import语法是否可以正确转换为 ES5 commonjs“require”模块格式(这是使用 Node 0.10.x)。

Oh as additional context, my tsconfig.jsonis:

哦,作为额外的上下文,我tsconfig.json是:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "removeComments": true,
    "sourceMap": true,
    "outDir": "./dist",
    "watch": true
  },
  "compileOnSave": true
}

Note: I've seen other people have gotten the "cannot redeclare block-scoped variable"error before but none of the conversations seemed to really fully fit my situation. Although i'm quite new to Typescript so maybe I'm making a newbie mistake.

注意:我之前见过其他人收到“无法重新声明块范围变量”错误,但似乎没有一个对话真正完全适合我的情况。虽然我对打字稿很陌生,所以也许我犯了一个新手错误。



Also of note, I noticed some examples of a strange variant of commonjs and ecmascript module formats:

另外值得注意的是,我注意到了一些 commonjs 和 ecmascript 模块格式的奇怪变体的例子:

import up = require('./upload');

This is in contrast to how I'd normally write it as:

这与我通常将其写为:

const up = require('./upload');

When I use the "import" keyword, however, it complains that upload.tsis not a module:

但是,当我使用“import”关键字时,它会抱怨它upload.ts不是模块:

not a module

不是模块

回答by Daniel Rosenwasser

If we look at the current snapshot of moment.d.tson DefinitelyTyped, we'll see that it is a global script file (as opposed to a module) with a variable named moment:

如果我们查看on absoluteTyped当前快照moment.d.ts,我们将看到它是一个全局脚本文件(而不是模块),其中包含一个名为 的变量moment

declare var moment: moment.MomentStatic;

It's considered a global script file because it doesn't import or export anything.

它被视为全局脚本文件,因为它不导入或导出任何内容。

Right now yourfile is also a script file. That might not sound correct, but the idea is that TypeScript doesn't recognize requireon its own as an import. requirecould technically be any other function.

现在你的文件也是一个脚本文件。这听起来可能不正确,但其想法是 TypeScript 无法require自行识别为导入。require技术上可以是任何其他功能。

Instead, you'll need to use a specific syntax for this:

相反,您需要为此使用特定的语法:

import moment = require("moment");

All that does is tell TypeScript that this is actuallyan import, and is safe to transform into an import in CommonJS, AMD, UMD, SystemJS etc.

所做的只是告诉 TypeScript 这实际上是一个导入,并且可以安全地转换为 CommonJS、AMD、UMD、SystemJS 等中的导入。



As a side note, for TypeScript 2.0, we've updated these .d.tsfiles notto be global script files, and instead be authored as modules. If you see the declaration files for moment in the types-2.0branchin DefinitelyTyped, you can see the following lines:

作为一个侧面说明,为打字稿2.0,我们已经更新了这些.d.ts文件不是必须是全球性的脚本文件,而是被编写为模块。如果您在绝对类型types-2.0分支中看到了moment声明文件,您可以看到以下几行:

declare var moment: moment.MomentStatic;
export = moment;

Where export = momentis module system-agnostic way of writing module.exports = moment.

export = moment模块系统不可知的写作方式在哪里module.exports = moment