33

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 '...path...';

I got errors from compiler:

Could not find a declaration file for module '...path...'. '...path...' implicitly has an 'any' type.

After that it works OK, but i need to avoid this error in console.

How can I make compiler understand that import has any type without creating declaration file?

Thanks for ideas in advance.

dekajoo
  • 2,024
  • 1
  • 25
  • 36
Max Minin
  • 453
  • 1
  • 4
  • 8
  • You do not show what `...path...` is. So maybe [Could not find a declaration file for module ... implicitly has an 'any' type](http://stackoverflow.com/questions/41292559) will help. – Wolfgang Kuehn Dec 30 '16 at 20:25

3 Answers3

29

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.

dekajoo
  • 2,024
  • 1
  • 25
  • 36
  • 27
    As an addition. In case of a public module that has no published typings, one workaround might be just ignoring the error: add a line with `// @ts-ignore` before the line with `import`. This feature is available since [TypeScript 2.6 (released on Oct 31, 2017)](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html#suppress-errors-in-ts-files-using--ts-ignore-comments) and should only be used as a last resort. – stsloth Aug 12 '18 at 13:43
  • 1
    This failed for me with "File C:/blah/blah/blah/name_of_the_module.d.ts is not a module." See [my similar question](https://stackoverflow.com/questions/55795060/how-to-import-js-in-ts-with-noimplicityany-and-without-allowjs) for details. – crenshaw-dev Apr 22 '19 at 13:30
  • @deKajoo, so for a axios.js file like below, I change the file name to axios.d.ts?Would I need to change the reference(import axios from "./axios.js") also in the main.js? import axios from 'axios'; export default axios.create({""}); – Ajit Goel May 06 '20 at 05:03
  • I don't know how the axios lib is defined. It could be: `import axios from 'axios';` or `import * as axios from 'axios';` Keep in mind that you are only importing definition files here so you should comply with how to lib works when creating your .d.ts. Also axios seems ot have a .d.ts in the lib so you should not need this: https://github.com/axios/axios/blob/master/index.d.ts you probably just need to `npm install axios` that's it – dekajoo May 06 '20 at 07:35
  • @deKajoo, The ./axios.js file is defined like `import axios from 'axios'; export default axios.create({""});`. In this case, would I change the `axios.js` file name to `axios.d.ts`?Would I need to change the reference(import axios from "./axios.js") also in the main.js? As you can see, these are newbie Typescript questions. – Ajit Goel May 06 '20 at 16:56
  • Open a detailed question on your issue with the differents .ts, .js and .d.ts files as well as the way you're building it and ping me on it if you want I'll have a look :) – dekajoo May 07 '20 at 09:15
16

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

declare module '*';

or you can ignore some specific imported libraries:

declare module 'name-of-the-lib';
skantus
  • 943
  • 10
  • 21
4

followup on skantus answer, make sure you include the global.d.ts in tsconfig.json includes array,

// in tsconfig.json
 
 "include": [
    "src",
    "src/models/common/global.d.ts"
  ]

// in src/models/common/global.d.ts
declare module '*'; // this will ignore all the js import errors
declare module 'diagram-js'; // or instead of ignoring all, you can ignore specific modules like this
Viraj Singh
  • 1,951
  • 1
  • 17
  • 27