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

Locate the package.json file while executing an npm script during the preinstall phase

Before installing a new npm package, it is essential to read the package.json. The Importance of Reading package.json When using npm for CSS components with individual versioning and possible interdependencies (without JavaScript), conflicts may arise. D ...

Why is the Last Page display on pagination showing as 3 instead of 2 per items?

When working on Angular 13, I encountered an issue with applying pagination numbers like 1, 2, 3, etc. The problem I faced was that the last page Number should be 2, but it is displaying as 3. Why is this happening? To investigate the issue, I tested my ...

Display React popup based on unique IP address only once

After implementing a popup section using the "React-js Popup package," I am interested in customizing it to display only once per User IP address. This means that if the same user revisits my website, the pop-up should not be shown to them again. ...

Tips for waiting for an observable loop

When using my application, the process involves uploading a series of images in order to retrieve the file IDs from the system. Once these IDs are obtained, the object can then be uploaded. async uploadFiles(token: string):Promise<number[]> { let ...

Prisma Remix is throwing a TypeError: "The function (0, import_prisma.createNote) is not defined as a function."

In my project, I wrote a function using the prisma client which is being called from the notes.tsx route in remix. export async function createNote(entity: { title: string, description: string }) { const note = await prisma.note.create({ data: ...

How can I prevent downloading the same node modules multiple times for my "sandbox" in npm?

I have a folder called "Sandbox" where I test different node repositories. Each repository has its own package.json file that requires me to run npm in order to install the necessary dependencies. This results in duplicate dependency modules being installe ...

Inject SCSS variables into Typescript in Vue 2 Using Vue-cli 5

When working on my Vue 2 project (created using the Vue-cli v4), I successfully imported variables from my SCSS file into my typescript (.vue files) without any issues. I had the :export { ... } in my SCSS file _variables.scss, along with shims.scss.d.ts ...

Utilizing a string variable as a property name for dynamic typing

I am looking to dynamically create a type with a property name based on specified parameters. Although I can successfully create the object, I am facing challenges when trying to create the actual type. This dynamic type creation is essential for compositi ...

What is the best way to integrate npm packages into my Laravel application?

I have been struggling to successfully install various packages on my project. Let's say I am attempting to integrate the Wysimark editor. Following the documentation, I execute npm i --save @wysimark/standalone. Then, in resources/js/app.js, I includ ...

Struggling with setting up the onChange function in a Next.js application

After successfully writing and testing the code here, I encountered an error preventing me from building it. Please review the code for any issues. I am attempting to set onChange to handle user input in a text field. Currently using onChange={onChange}, ...

Angular 5 Directive for Structuring Content

I'm currently in the process of developing a versatile search box component, with the following setup: search.component.html <div class="search-box-container"> <fa-icon class="search-icon" [icon]="faSearch"></fa-icon> <input ...

How should dynamic route pages be properly managed in NextJS?

Working on my first project using NextJS, I'm curious about the proper approach to managing dynamic routing. I've set up a http://localhost:3000/trips route that shows a page with a list of cards representing different "trips": https://i.stack. ...

Error encountered while trying to install Appium using npm

Encountering an error while trying to install appium using npm. Despite having gulp installed, the issue persists. Has anyone else experienced similar problems? <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80e1f0f0e9f5ed ...

What specific element is being targeted when a directive injects a ViewContainerRef?

What specific element is associated with a ViewContainerRef when injected into a directive? Take this scenario, where we have the following template: template `<div><span vcdirective></span></div>` Now, if the constructor for the ...

Issue: React cannot render Objects as children (received: [object Promise]). If you intended to display multiple children, please use an array instead. (Next)

My dilemma is this: I am trying to display my GitHub repositories on a React component, but I keep encountering the following error: Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, u ...

When utilizing a Service through UserManager, the User variable may become null

Utilizing Angular 7 along with the OIDC-Client library, I have constructed an AuthService that provides access to several UserManager methods. Interestingly, when I trigger the signInRedirectCallback function from the AuthService, the user object appears ...

Verification of Username using Validator (npm)

I am currently in the process of developing a web app using Node.js. As part of validating inputs, I am utilizing the validator package. My goal is to only permit alphanumeric characters, while also allowing periods, underscores, and hyphens. (. _ -) Bel ...

"What is the best way to calculate the total value of an array in TypeScript, taking into account the property

I'm currently working on a small Angular project that involves managing an array of receipt items such as Coke, Fanta, Pepsi, Juice, etc. Each receipt item has its own price and quantity listed. receiptItems: Array<ReceiptItem>; Here is the st ...

Utilizing global enumerations within VueJS

Is there a way to effectively utilize global enums in Vue or declare them differently? My current setup is as follows: Within my types/auth.d.ts: export {}; declare global { enum MyEnum { some = "some", body = "body", o ...

Module '. ' or its corresponding type declarations cannot be located

While working on my project with TypeScript and using Cheerio, I encountered an issue when trying to compile it with TSC. The compiler threw the following exception: error TS2307: Cannot find module '.' or its corresponding type declarations. 2 ...