Typescript gives, “Could not find a declaration file for module 'xmlhttprequest'.”

无人久伴 提交于 2019-12-20 06:09:59

问题


Using,

import { XMLHttpRequest } from 'xmlhttprequest';

On Node I get the following error when I compile with tsc

index.ts|4 col 32 error| 7016[QF available]: Could not find a declaration file for module 'xmlhttprequest'. '<project>/node_modules/xmlhttprequest/lib/XMLHttpRequest.js' implicitly has an 'any' type. Try npm install @types/xmlhttprequest if it exists or add a new declaration (.d.ts) file containing declare module 'xmlhttprequest';

However, that package doesn't seem to be there,

npm install @types/xmlhttprequest
npm ERR! code E404
npm ERR! 404 Not Found: @types/xmlhttprequest@latest

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ecarroll/.npm/_logs/2018-07-31T00_19_20_299Z-debug.log

Is there a way anything that packages this type?


回答1:


Can you try to do npm install @types/xmlhttprequest --save in your command line.

If it gives you error, then it means that xmlhttprequest library does not support TypeScript.




回答2:


That means that the library does not contain type definitions and nobody from the DefinitelyTyped project wrote them. So you can't have the compiler do any typechecking. You can still use the library either

import * as xhr from 'xmlhttprequest'

Or, depending on version of your compiler

const xhr = require('xmlhttprequest')


来源:https://stackoverflow.com/questions/51603854/typescript-gives-could-not-find-a-declaration-file-for-module-xmlhttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!