导入声明的 TypeScript 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33802519/
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:08:12  来源:igfitidea点击:

TypeScript issue with importing declarations

typescript

提问by uksz

I am having an issue with importing declarations from an extended file (I am using thistyping). According to example, I should put this into my code:

我在从扩展文件导入声明时遇到问题(我正在使用这种类型)。根据示例,我应该将其放入我的代码中:

import * as SockJS from 'sockjs-client';
import BaseEvent = __SockJSClient.BaseEvent;
import SockJSClass = __SockJSClient.SockJSClass;

However, when I try to do this as following:

但是,当我尝试按以下方式执行此操作时:

module Test {
    import * as SockJS from 'sockjs-client';
    import BaseEvent = __SockJSClient.BaseEvent;
    import SockJSClass = __SockJSClient.SockJSClass;

    export class Example {
        constructor() {......
}}}

I get the following error from the compiler:

我从编译器收到以下错误:

error TS1147: Import declarations in a namespace cannot reference a module.

Am I doing something wrong? Or is there any issue with the typing itself?

难道我做错了什么?还是打字本身有问题?

Thanks
uksz

谢谢
uksz

回答by Andzej Maciusovic

You should use your import statements outside your module

您应该在模块之外使用导入语句

import * as SockJS from 'sockjs-client';
import BaseEvent = __SockJSClient.BaseEvent;
import SockJSClass = __SockJSClient.SockJSClass;

module Test {
    export class Example {
        constructor(){}
    }
}

回答by Bj?rn S?rensen

I believe this is due to a mix of the typescript module options.

我相信这是由于 typescript 模块选项的混合。

Your class uses internal modules and the typing file uses external modules. See the Working with other JavaScript libraries section here: http://www.typescriptlang.org/docs/handbook/modules.html

您的类使用内部模块,类型文件使用外部模块。请参阅此处的使用其他 JavaScript 库部分:http: //www.typescriptlang.org/docs/handbook/modules.html

回答by Juniuz

I have faced the same issue when trying to import moment.d.tstype definition file in one of my typescript file.

尝试moment.d.ts在我的打字稿文件之一中导入类型定义文件时,我遇到了同样的问题。

I'm also wrapping my whole class inside a module. The solution that I did to solve the issue was, in my typescript file - Scheduler.ts, I put the line import * as moment from "../definitions/moment/moment";just before the moduledeclaration (refer to image below).

我还将整个班级都包装在一个module. 我为解决这个问题所做的解决方案是,在我的打字稿文件 - 中Scheduler.ts,我将这一行放在声明import * as moment from "../definitions/moment/moment";之前module(参见下图)。

I didn't include the whole class definition for brevity reason.

为简洁起见,我没有包含整个类的定义。

enter image description here

enter image description here

As you can see, I have reference pathexplicitly define in the typescript file for external libraries (jquery, kendo uiand moment).

如您所见,我reference path在打字稿文件中明确定义了外部库(jquery,kendo uimoment)。

The folder structure where type definitions are saved.

保存类型定义的文件夹结构。

enter image description here

enter image description here

Below is also my tsconfig.json, not quite sure setting this allowSyntheticDefaultImports: trueease the problem. I just followed the notes written on this linkwhen importing and using in typescript file.

下面也是我的tsconfig.json,不太确定设置这个可以 allowSyntheticDefaultImports: true缓解问题。在打字稿文件中导入和使用时,我只是按照此链接上写的注释进行操作。

    {
  "compileOnSave": true,
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "module": "commonjs",
    "strictNullChecks": false,
    "allowSyntheticDefaultImports": true
  }
}