How to import node module in Typescript without type definitions?

前端 未结 4 1300
[愿得一人]
[愿得一人] 2021-02-20 12:16

When I try to import node.js module in Typescript like this:

import co = require(\'co\');
import co from \'co\';

without providing type definit

4条回答
  •  北海茫月
    2021-02-20 12:41

    I got an error when I used the "Stubbing type definitions" approach in Tim Perry's answer: error TS2497: Module ''module-name'' resolves to a non-module entity and cannot be imported using this construct.

    The solution was to rework the stub .d.ts file slightly:

    declare module 'module-name' {
      const x: any;
      export = x;
    }
    

    And then you can import via:

    import * as moduleName from 'module-name';
    

    Creating your own stub file lowers the barrier to writing out real declarations as you need them.

提交回复
热议问题