Creating a TypeScript declaration file for a singular module

Consider the following directory structure:

src/
├── foo.ts
├── bar.ts
├── baz.ts
├── index.ts

If foo.ts, bar.ts, and baz.ts each export a default class or object, for example in foo.ts:

export default class Foo {
    x = 2;
}

Is it possible to automatically create a declaration file that defines a module named my-module and exports foo.ts, bar.ts, and baz.ts as non-defaults?

In other words, I would like tsc to generate the following:

build/
├── foo.js
├── bar.js
├── baz.js
├── index.js
├── index.d.ts

Where index.d.ts includes:

declare module 'my-module' {
    export class Foo {
        ...
    }
    export class Bar {
        ...
    }
    export class Baz {
        ...
    }
}

It seems that most NPM modules have a declaration file structured in a similar way.

How can I achieve this?

Answer №1

To enable automatic generation of declaration files in your project, simply update the "declaration" setting to true in your tsconfig.json file before running typescript.

Answer №2

tsup-unique

Not only does it have the capability, but it's also incredibly quick (1.2s):

tsup-unique ./executors/index.ts --dts-only

Make sure to include and export the types from the ./executors/index.ts file.

dts-bundle-generator-unique

In my experience, this one was a bit slower (9.0s):

dts-bundle-generator-unique --out-file dist/testkube-executors.d.ts ./executors/executors.ts

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

The utilization of the Angular date pipe significantly impacts the way dates are

When I use the pipe date:'MM/dd/YYYY' to display the date 2022-01-01T00:00:00, it shows as 1/01/2021 instead of 1/01/2022. This issue only occurs with this specific date. Why does this happen? The value of pharmacyRestrictionDate is 2022-01-01T0 ...

Determining the data type of a property within an interface using TypeScript

Is there a way to extract the type from an interface based on its property name in order to use it in a Record? I am struggling with the syntax needed to retrieve the type by property name. My goal is to make this process more future-proof so that if the i ...

The CSS files undergo modifications when executing the command "npm run dev"

I've been working on an open-source project where I encountered a bug. Even when there are no images to display, the "Load More" button in the web browser extension still appears. To fix this, I decided to add the class `removeButton` to the button an ...

What causes the Cassandra client driver to provide more detailed information compared to cqlsh?

I'm currently utilizing the datastax nodejs-driver to retrieve information about a keyspace from cassandra. const results = await client.execute( ` DESC KEYSPACE ${keyspace} ` ); The method client.execute provides a comprehensive object containin ...

Completely remove detox from MacOS by uninstalling it

I am looking for the best way to fully remove Detox from my Mac running macOS 10.14.6 (18G5033). After uninstalling the global detox-cli using 'npm uninstall -g detox-cli', I noticed there is still a directory located at ~/Library/Detox/ contain ...

After using browserify, when attempting to call the function in the browser, an Uncaught ReferenceError occurs

I am currently in the process of creating a compact NPM package. Here is a basic prototype: function bar() { return 'bar'; } module.exports = bar; This package is meant to be compatible with web browsers as well. To achieve this, I have inst ...

Issues with command functionality within the VS Code integrated terminal (Bash) causing disruptions

When using Visual Studio Code's integrated terminal with bash as the shell, I have noticed that commands like ng and tsc are not recognized. Can anyone shed some light on why this might be happening? ...

Error 404: Unable to locate protobufjs version 5.0.0

My attempt to incorporate firebase into my project is met with an error: >npm install -S firebase npm ERR! code E404 npm ERR! 404 Not Found: protobufjs@^5.0.0 npm ERR! A complete log of this run can be found in: npm ERR! /Users/my_username/.npm/_l ...

Ways to retrieve a Class Level Variable within the onCellValueChanged function in agGrid

Exploring Angular with Typescript for the first time. I'm trying to access Class Level Variables within the onCellValueChanged event of agGrid, but encountering the following error: Cannot read property 'updateDict' of undefined Here&apo ...

Execute TSC on the Hosted Build Agent

Currently, I am diving into TypeScript and have managed to create a basic app that I would like to deploy using VSTS on Azure App Service. My straightforward build definition involves the following steps: Utilize "Node Tool Installer (preview)" to set up ...

Remove all input fields within an HTML file using a TypeScript method implemented in an Angular 2 component

Within my Angular project, there are several input elements in the HTML file that are not enclosed within a form tag. I am looking to create a function in the TypeScript file that will clear all of these inputs. I attempted to utilize ViewChild, but it a ...

Perform the subtraction operation on two boolean values using Typescript

I'm working with an array: main = [{ data: x, numberField: 1; }, { data: y, numberField: 2; }, { data: x, numberField: 3; }, { data: z, numberField: 4; }, { data: ...

Encountering the error message 'array expected for services config' within my GitLab CI/CD pipeline

My goal is to set up a pipeline in GitLab for running WebdriverIO TypeScript and Cucumber framework tests. I am encountering an issue when trying to execute wdio.conf.ts in the pipeline, resulting in this error: GitLab pipeline error Below is a snippet of ...

What steps should I take to resolve the error message "ESLint encountered an issue determining the plugin '@typescript-eslint' uniquely"?

Struggling to enable eslint linting in an ASP.NET Core MVC project that incorporates React.js and typescript? I'm facing a tough challenge trying to resolve the error mentioned above. In my setup, I'm using Visual Studio 2022 Community Edition 1 ...

The Angular application remains unminified after being built with the "prod" flag

Currently, my Angular 7 project is configured for production build with the settings optimization=true and buildOptimizer=true. Below, you will find a snippet of the full production configuration from my angular.json file. I have set up an automated build ...

Is there a way to utilize the 'interval' Rxjs function without triggering the Change Detection routine?

My goal is to display the live server time in my application. To achieve this, I created a component that utilizes the RXJS 'interval' function to update the time every second. However, this approach triggers the Change Detection routine every se ...

Issue: main@HEAD Failed during 'site building' stage: Build script ended with non-zero exit code: 2

11:29:48 AM: build-image version: 3d3c7e8b4321e2c1a54a2c4584fb46ba742b1630 (focal) 11:29:48 AM: buildbot version: 72ed9578274f76ae72cdce4c5312615aeecc32fb 11:29:49 AM: Building without cache 11:29:49 AM: Starting to prepare the repo for build 11:29:49 AM: ...

Error message 'require is not defined' can occur in Meteor.js when trying to incorporate an NPM package

I'm facing an issue while trying to utilize an npm package in Meteor.js (Release 0.6.6.3) by using Meteor.require. The error thrown states that require is not defined. What could be causing this and how can it be resolved? mrt add npm npm install git ...

Issue encountered while executing npm run dev within GitHub Action workflows

I'm facing an issue while trying to link a GitHub repository to cPanel. I've set up the secrets, created the .github/workflows/main.yml file, and executed it. However, upon checking my GitHub action, it seems to be malfunctioning. Below are the e ...

Angularjs 2 Error: Unable to access the 'infos' property of an undefined object using the Http Client

I've been working on an AngularJS app for about a week now, developing a backoffice application for my service. My main challenge lies in using data retrieved from a remote server. I have 4 HTTP GET requests in my app - 2 of them fetching lists of us ...