简体   繁体   中英

TypeScript: import an ES2015 module without type definitions

I have a working ES2015 project, which I would like to migrate to TypeScript.

In my solution I am using ES2015 imports to import 3rd-pary modules (installed via npm) like this:

import {ClassXY} from 'moduleXY'

After setting up TypeScript in my project, the TypeScript compiler reports the following error:

error TS2307: Cannot find module 'moduleXY'

How can I get the above ES2015 import statement working in TypeScript?

I am looking for a solution other than installing the corresponding .d.ts declarations via typings ... let's assume the module moduleXY is a library that does not distribute its own .d.ts declarations and there are no declarations available on DefinitelyTyped (or the declarations are outdated).

Must I create a stub for the type definitions of module moduleXY ? How would I do that with minimal effort?

if there are no typings for a module then you can just require it like a regular commonjs module

declare require:any;

const moduleXY:{
    classXY:any
} = require("moduleXY");

in a .ts file and make sure that file is included with your source in the Typescript compiler. you don't need to fully declare the module, and you will still benefit in places where you start adding types moving forward.

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