TypeScript 类型给我“index.d.ts 不是模块”

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

TypeScript typings give me "index.d.ts is not a module"

typescripttypescript-typings

提问by Tomá? Hübelbauer

I am getting File node_modules/@types/webrtc/index.d.ts is not a modulewith this code:

我得到文件 node_modules/@types/webrtc/index.d.ts is not a modulewith this code:

import * as webrtc from "webrtc";
const peerConnection1 = new RTCPeerConnection();

I have installed the typings using npm i @types/webrtc --save-dev. Hovering over RTCPeerConnectionin const peerConnection1 = new RTCPeerConnection();display type annotations in Visual Studio Code so at least the code editor sees the types. Running tsc(or webpackwith ts-loader) fails with the error.

我已经使用npm i @types/webrtc --save-dev. 将鼠标悬停在 Visual Studio CodeRTCPeerConnection中的const peerConnection1 = new RTCPeerConnection();显示类型注释中,以便至少代码编辑器可以看到类型。运行tsc(或webpackwith ts-loader)失败并显示错误。

I have tried npm i webrtc --savein a misguided attempt for solving this, but it did not change anything and I really only want the typings anyway, WebRTC is right there in the browser, I don't need a package for that. (Support aside.)

我已经尝试npm i webrtc --save过一次错误的尝试来解决这个问题,但它没有改变任何东西,而且我真的只想要打字,WebRTC 就在浏览器中,我不需要一个包。(支持一边。)

The index.d.tsfile indeed is not a module, it just references two other files with interfaces in them. So I thought to remove import * as webrtc from "webrtc";hoping the typings will still be visible by tscsomehow. (But that's impossible since I exclude node_modulesin TypeScript config file.) When I do that RTCPeerConnectionis no longer recognized.

index.d.ts文件确实不是一个模块,它只是引用了另外两个带有接口的文件。所以我想删除import * as webrtc from "webrtc";希望打字仍然以tsc某种方式可见。(但这是不可能的,因为我node_modules在 TypeScript 配置文件中排除了。)当我这样做时RTCPeerConnection不再被识别。

Adding /// <reference src="node_modules/@types/webrtc/" />did not help, tscsays Invalid reference directive syntax.

添加/// <reference src="node_modules/@types/webrtc/" />没有帮助,tsc无效引用指令语法

You can view a repository with minimal repro here on GitLab. I am not too well versed in TypeScript typings acquisition so please forgive my ignorance if I'm going about this all wrong.

您可以在 GitLab 上查看具有最少重现的存储库。我不太熟悉 TypeScript 类型的获取,所以如果我把这一切都弄错了,请原谅我的无知。

回答by Meirion Hughes

webrtc is part of the browser; you're trying to import a module. Simply import the (typings) library:

webrtc 是浏览器的一部分;您正在尝试导入模块。只需导入(类型)库:

import "webrtc";

you may need to use "moduleResolution": "node"in the compiler options.

您可能需要"moduleResolution": "node"在编译器选项中使用。

Alternatively use the "types": ["webrtc"]compiler option and the compiler will automatically load those types up for you.

或者使用"types": ["webrtc"]编译器选项,编译器会自动为您加载这些类型。

回答by Daniel Rosenwasser

You probably want to add

你可能想添加

"types": ["webrtc"]

to your tsconfig.json, or less preferrably, to use

tsconfig.json,还是少preferrably,在使用

/// <reference types="webrtc" />

in your source files. Here's an example of it in your tsconfig.json:

在您的源文件中。这是您的示例tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "sourceMap": true,
        "noImplicitAny": true,

        "types": ["webrtc"]
    },
    "exclude": [
        "node_modules"
    ]
}

This tells TypeScript it should include webrtcdeclarations in your build

这告诉 TypeScript 它应该webrtc在你的构建中包含声明

回答by Sabir Hussain

No need to import anything, run following:

无需导入任何东西,运行以下命令:

  1. npm install --save @types/webrtc
  2. update tsconfig.json -

    "types": [ "@types/webrtc" ]

  1. npm install --save @types/webrtc
  2. 更新 tsconfig.json -

    “类型”:[“@types/webrtc”]

回答by Karolis Grinkevi?ius

Another option is to add a new declaration file *.d.tsin your module, i.e.:

另一种选择是*.d.ts在您的模块中添加一个新的声明文件,即:

declare module 'my-module';

回答by youngrrrr

/// <reference types="@types/<your_types_module>" />

/// <reference types="@types/<your_types_module>" />

You may or may not want to do this depending on your build and style needs, but this seems to be the quick (and dirty) fix.

根据您的构建和样式需求,您可能想也可能不想这样做,但这似乎是快速(和肮脏)的解决方法。