In my development setup, I am utilizing Webpack along with @babel/typescript
to compile a project that consists of a mix of TypeScript and JavaScript.
To ensure better typing and take advantage of the benefits it offers, I have enabled the --noImplicitAny
flag in my configuration.
However, due to the size of my project, I decided against using --allowJs
as it caused performance issues with the TypeScript compiler and interfered with Visual Studio highlighting and Intellisense.
When dealing with npm modules that are not typed, if time constraints prevent me from adding typings, I create a definition file where I explicitly set the type to any
. For example
example.ts
import * as elemDataset from 'elem-dataset';
elem-dataset.ts
declare module 'elem-dataset';
This approach satisfies the compiler. However, for internal modules that have not been converted to TypeScript yet...
import * as example from './example2'; // Where example2 is example2.ts
An error is triggered:
Could not find a type declaration file for module './example2'. C:/blah/blah/blah/example2.js implicitly has 'any' type.
In an attempt to address this issue, I followed the solution provided in this answer.
example2.d.ts
declare var example2: any;
declare module "example2" {
export = example2;
}
However, a new error emerged:
File C:/blah/blah/blah/example2.d.ts is not a module.
Another approach I tried was using declare module '*';
as suggested in this answer, but it resulted in the same error as before.
My question remains: How can I explicitly define the import type of an internal JavaScript file as any?