Typescript: Ignore implicitly any type when importing js module

前端 未结 2 1470
旧时难觅i
旧时难觅i 2021-02-07 07:41

In Typescript project I need to import some old js files that do module.exports inside of themselves.

As i import:

import * as httpConnection from \'...p         


        
相关标签:
2条回答
  • 2021-02-07 08:26

    This mean you should type this module. Typescript is not happy with this module implicitly not having any definition file.

    If it's an external public module you might just be able to install its types with a npm install @types/name_of_the_module

    (Some modules even have their definition files inside the lib now. So just npm installing the module gives you the .d.ts definitions. But here that's probably not the case)

    If it's private code or if you can't find it you could add a .d.ts file next to it that would ideally type all of its functions. If your willing to sacrifice typing because you don't have time to do it you can just have an explicit any type on the module with the following .d.ts

    declare var name_of_the_module: any;
    
    declare module "name_of_the_module" {
        export = name_of_the_module;
    }
    

    Edit:

    As pointed out by @stsloth as for Typescript 2.6 you can also ignore the error completely by adding the line // @ts-ignoreabove the import.

    0 讨论(0)
  • 2021-02-07 08:44

    You could create a file named: global.d.ts in the root of your project and add this:

    declare module '*';
    
    0 讨论(0)
提交回复
热议问题