TypeScript 类型给我“index.d.ts 不是模块"; [英] TypeScript typings give me "index.d.ts is not a module"

查看:293
本文介绍了TypeScript 类型给我“index.d.ts 不是模块";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到文件 node_modules/@types/webrtc/index.d.ts is not a module 使用此代码:

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

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

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

我尝试了 npm i webrtc --save 以误导性的尝试来解决这个问题,但它没有改变任何东西,无论如何我真的只想要打字,WebRTC 就在浏览器中,我不需要那个包.(暂不支持.)

I have tried npm i webrtc --save in 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.)

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

The index.d.ts file 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 tsc somehow. (But that's impossible since I exclude node_modules in TypeScript config file.) When I do that RTCPeerConnection is no longer recognized.

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

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

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

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.

推荐答案

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

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

import "webrtc";

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

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

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

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

这篇关于TypeScript 类型给我“index.d.ts 不是模块";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆