Import from installed @types?

后端 未结 1 497
感情败类
感情败类 2021-01-23 10:23

I\'ve installed md5 (also tried blueimp-md5) package with corresponding typings like this:

nmp install --save md5 @types/md5

1条回答
  •  北海茫月
    2021-01-23 10:47

    You don't need to specify the path inside the node_modules, it should be:

    import * as md5 from "md5";
    

    The compiler will look for the actual module in the node_modules, and will look for the definition files in node_modules/@types.

    There's a long doc page about it: Module Resolution


    Edit

    That's because of how the md5 module is exporting, as it does this:

    declare function main(message: string | Buffer): string;
    export = main;
    

    This case is covered in the docs:

    The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

    When importing a module using export =, TypeScript-specific import let = require("module") must be used to import the module.

    In your case it should be:

    import md5 = require("md5");
    

    2nd edit

    If you're targetting es6 then you need to do:

    const md5 = require("md5");
    

    (or let or var of course).

    0 讨论(0)
提交回复
热议问题