Guide on importing JavaScript into TypeScript without using noImplicityAny and while not allowing Js

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?

Answer №1

It turns out that this simple solution met all of my requirements:

// @ts-ignore
import * as x from './example2';

Initially, I was hesitant to try this approach because I believed that adding a comment would disable type checking for the entire file.

However, by using @ts-ignore just before the import statement, I can maintain type checking for the rest of my project while temporarily ignoring a single JavaScript file until I can convert it later on.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve a formatted item from a JSON document

Within my Next.js project, I have implemented a method for loading translations and passing them into the component. Here is an example: import "server-only"; import i18nConfig from "../../i18n-config"; const dictionaries = { en: () ...

Building a Model Class with redux-orm and TypeScriptCreating a new Model Class with

I've been successfully using redux-orm with JavaScript, however, I'm facing issues when trying to convert my code to TypeScript. Even though I have already implemented the render() method in my Folder Model Class, the TypeScript transpiler is sh ...

How can you ensure an interface in typescript 3.0 "implements" all keys of an enum?

Imagine I have an enum called E { A = "a", B = "b"}. I want to enforce that certain interfaces or types (for clarity, let's focus on interfaces) include all the keys of E. However, I also need to specify a separate type for each field. Therefore, usi ...

Utilize Babel-Plugin-React-Css-Modules to incorporate external library stylesheets

I am struggling to integrate another library's CSS for their component into my own application. I am attempting to utilize a data table library from the following source: https://github.com/filipdanic/spicy-datatable. According to the documentation, ...

Encountering issue in Angular 14: Unable to assign type 'Date | undefined' to type 'string | number | Date' parameter

I have been working on an Angular 14 project where I am implementing a Search Filter Pipe. Below is the code snippet I am using: import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'transferFilter' }) export class Trans ...

Error: The FactoryMethod.render() function requires a valid React element to be returned, or null

An error has occurred: Error: FactoryMethod.render(): A valid React element (or null) must be returned. You may have returned undefined, an array, or some other invalid object. at invariant (react-dom.js:17896) Despite everything being fine during co ...

Encountering a module not found error when attempting to mock components in Jest unit tests using TypeScript within a Node.js

I'm currently in the process of incorporating Jest unit testing into my TypeScript-written Node.js application. However, I've hit a snag when it comes to mocking certain elements. The specific error I'm encountering can be seen below: https ...

Encountering an issue with applying D3 fill to a horizontal stacked bar chart in Angular using TypeScript. When using .attr("fill", ..) in VSC, an error stating "No overload matches this call" is displayed

My goal is to create a stacked horizontal bar chart in d3, and I've been following the code example provided here. To showcase my progress so far, I have set up a minimal reproduction on stackBlitz which can be found here. While there are no errors ...

Exploring the power of flow.js within an Angular 2 Typescript project

I am trying to incorporate flowjs or ng-flow into my Angular 2 application. After installing the flowjs typings using npm install --save-dev @types/flowjs from However, upon importing it into my component with import { Flow } from 'flowjs';, ...

"encountered net::ERR_NAME_NOT_RESOLVED error when trying to upload image to s3 storage

I am currently developing an application using Angular. I have been attempting to upload a picture to my S3 bucket, but each time I try, I encounter this error in the console. https://i.stack.imgur.com/qn3AD.png Below is the code snippet from my upload.s ...

Assign an appropriate label to this sonarqube input field

Sonarqube flagged an issue with the following line of code: <div class="dropdown-language"> <label>{{'GENERALE.LINGUA' | translate }}</label> <select #langSelect (change)="translate.use(langSe ...

Dragula drag and drop in a single direction with Angular 2 copy functionality

Attempting to utilize ng2 dragula for one-way drag and drop with copy functionality. Below is the template I am working with: `<div> <div class='wrapper'> <div class='container' id='no-drop' [dragula]=& ...

Utilize PrimeNG's async pipe for lazy loading data efficiently

I have a significant amount of data (400,000 records) that I need to display in a PrimeNG data table. In order to prevent browser crashes, I am looking to implement lazy loading functionality for the table which allows the data to be loaded gradually. The ...

The specified property cannot be found on the Window type and the globalThis typeof

I encountered an error in my Electron-React-Typescript app that reads: Property 'api' does not exist on type 'Window & typeof globalThis'. window.api.send('open-type-A-window', ''); The issue seems to be related ...

Custom "set attribute" feature in TypeScript

One issue I faced was resolved by creating the function shown below : function setProperty<T extends Record<string, string>>(obj: T, key: keyof T) { obj[key] = "hello"; } However, when I tried to compile the code, I encountered an ...

Must run the angular code in a sequential order

I need to run the code in a specific order; first the foreach loop should be executed, followed by a call to the getHistory() method. Your assistance is greatly appreciated. const execute = async()=>{ await this.currentContent.forEach(async ...

Moving the starting directory of a NodeJS application on Azure

My NodeJS app on Azure was initially written in Javascript with the app.js file located in the root directory. This file was automatically detected during deployment via Git. Recently, I converted the app to Typescript and now have a build directory, with ...

What is the proper way to nest a type alias array within another type alias in TypeScript code?

type subProperty = { identifier: string, label: string }; type MainParentProps = { subscriptionList?: [ { identifier: string, content: [???Collection Of subProperty???] } ] } Can this scenario be implemented in typescript usin ...

Retrieving the attribute key from a dynamically typed object

Having this specific interface structure: interface test { [key: string]: string } along with an object defined as follows: const obj: test ={ name: 'mda', telephone: '1234' } Attempting to utilize this object in a variab ...

What is the method to verify the BASE_DIR in Django?

I encountered numerous file not found errors for webpack within my static folder, only to realize that this specific project (which I am using docker with) kept pointing to another project's static folder for the required files. I am puzzled as to whe ...