简体   繁体   中英

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

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?

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM