Is it necessary to encapsulate the last Typsecript export in a module?

I am facing the challenge of exporting multiple models and services within a TypeScript module.

Origin

models/User.ts

import {IModel} from "./IModel";

export interface User extends IModel {
    // ...
}

services/UserService.ts

import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import {BaseService} from "./BaseService";
import {User as ModelType} from "../models/User";

@inject(HttpClient)
export class UserService extends BaseService<ModelType> { 
    // ...
}

Output

This is how I envision the final product after project compilation. While not an exact replica, my goal is for the root module my-module to have distinct sub-modules for models and services. This structure will prevent any potential naming conflicts between models and services.

dist/index.d.ts

declare module "my-module/models" {
    export interface User { ... }
    // ... other models here ...
}

declare module "my-module/services" {
    export class UserService { ... }
    // ... other services here ...
}

Ultimately, this package can be installed via npm and utilized in another project like so:

import {User} from "my-module/models";
import {UserService} from "my-module/services";
  • Due to the large number of models and services, manually maintaining an index.ts to handle them all would be inefficient.
  • What would be the best way to organize my TypeScript project to achieve this desired structure?

Answer №1

If you're looking to streamline your declaration files and dependencies, consider using the typings bundle command. This tool consolidates all necessary files into one for easier management.

For example, imagine a module named "stackoverflow" with files stackoverflow.ts and constants.ts. The index.ts file relies on external libraries like bluebird as well as constants.ts.

stackoverflow.ts

import { PromisifyOptions } from 'bluebird'
export { FIRST } from './constants'

export interface thePromiseUsingInterface {
  options: PromisifyOptions
}

constants.ts

export const FIRST = 1
export const SECOND = 2

To create a single declaration file:

  1. Generate declaration files using:tsc --declaration)
  2. Combine them with dependencies into index.d.ts using:typings bundle -o index.d.ts

When publishing your library on npm, remember to include typings.json.

In another project, install stackoverflow with:npm install stackoverflow and use it:

import * as constants from 'stackoverflow/constants'
console.log(constants.SECOND)

Here's a gist containing relevant files: stackoverflow.ts, constants.ts, index.d.ts, package.json, typings.

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

Looking to tally up the rows in a joined table using Sequelize?

In my current project, I am utilizing the sequelize library to interact with a sqlserver database. The database consists of two tables: data: columns - id, name, team, type history:columns - id, node, date, status, data_id (foreignkey) In terms of their ...

Creating a dynamic feature to add a row at the bottom of a table

I am currently working with JavaScript in the context of an AngularJS application, attempting to insert a row at the bottom of a table that shows the total sum of a specific column. Here is the code snippet I am using: var table = document.querySelecto ...

When HTML elements are dynamically inserted through JavaScript using quilljs, they may cause conflicts with the layout properties

I am currently working on creating a simple webpage layout similar to that of Stack Overflow, with a sidebar and a main content area that can scroll. In my case, the content area is intended to host a QuillJS text editor. To integrate the QuillJS editor i ...

The TextArea element is experiencing issues with the Jquery function

In my asp.net web application, I have implemented a JQuery function to restrict user input characters. $(document).ready(function () { $('input').on('input', function () { var c = this.selectionStart, ...

Unable to locate a declaration file for the 'mymodule' module

After attempting to import my test module by installing it with npm i github.com/.../..., the code is functioning properly. However, when I opened it in VSCode, an error message popped up: Could not find a declaration file for module 'estrajs'. & ...

What is the best way to implement Axios for data fetching in Gatsby's useStaticQuery function?

One way to fetch data in Gatsby is by using GraphQL, like in the following example: import { graphql, useStaticQuery } from "gatsby" const IndexPage = () => { const gatsbyRepoData = useStaticQuery(graphql` { github { repo ...

Is there a way to reactivate Cognito tokens that are originating from a different tab?

I have a unique feature that opens a new tab on a different domain, and I want to ensure the cognito session remains active. To achieve this, I've implemented a hidden iframe with the same origin to transfer local storage data using the following code ...

Creating TypeScript modules for npm

I have been working on creating my first npm module. In the past, when I used TypeScript, I encountered a challenge where many modules lacked definition files. This led me to the decision of developing my module in TypeScript. However, I am struggling to ...

Swiper: What methods can be used to classify the nature of an event?

Currently, I am utilizing Swiper for React in my project. I find myself in need of implementing a different effect when the user swipes versus using buttons to switch between active slides. Upon examining the swipe object for pertinent details regarding ...

Transfer information(text) to the clipboard using Vue (Nuxt js)

When the vs-button is clicked, I want the value of single_download_link.pdfId to be copied to the clipboard. I attempted it like this, but it did not work. I do not want to use the v-clipboard node module. Can someone please assist me with this? Thank you. ...

What are some ways to design scrollbars with a Google Wave-like style?

Is there a way to design scrollbars similar to the ones found in Google Wave? They seem to save space and have an appealing look. I am interested in implementing these customized scrollbars on a div element, just like how Google Wave utilizes them. https: ...

Troubleshooting Vue.js: Why is .bind(this) not behaving as anticipated?

Demo: https://codesandbox.io/s/23959y5wnp I have a function being passed down and I'm trying to rebind the this by using .bind(this) on the function. However, the data that is returned still refers to the original component. What could I be missing h ...

What is the process for converting the output of cryptoJS.sha256 to binary in a Postman pre-request script?

Seeking assistance in creating an HMAC signature using a pre-request script in Postman. While troubleshooting, it has become apparent that there is an issue with the signature generation process. Although a proof of concept example provides expected result ...

What is the best way to test a JavaScript function that includes nested timeouts using Jasmine?

I have a function that clears an input on blur. It's designed for use with Angular Materials, and I've created a directive for when this functionality is needed. function clearTextOnBlurLink(scope, element, attrs, controller) { $timeout(f ...

Displaying data from backend in an Angular 7 textbox

I'm currently working with Angular 7 and I have managed to retrieve values from the backend. However, I am struggling to display these values in a textbox. Can anyone provide me with some suggestions on how to achieve this? My tech stack includes Loo ...

storing multiple values in a single column of a PHP table

I am facing an issue while trying to generate a PDF from two tables in my database: 'book' and 'author'. The problem arises when a book has more than one author, as I am unable to display them in a single column. Below are images showin ...

I seem to be having some trouble with localStorage. Can anyone shed some light on why this might be

I'm having trouble with the code below and need some assistance to achieve the desired result. HTML: Name: <input type="text" id="name" name="name"></input> <button id="send" type=&qu ...

Why won't the jQuery function trigger when I click, but only responds when I move the cursor?

I am currently working on a website that includes a basic CSS style switcher. The function responsible for handling the theme button clicks is shown below: <script> $(function() { $(".light").click(function(){ $("link").attr("href", ...

Extracting information from JSON fixture within simulated Angular service

I recently developed a mock Angular service specifically for testing a controller. However, whenever I run my tests, an error pops up saying: Unexpected request: GET ./fixtures/stats.json. This is how the code looks like in mock.players.service.js: &apos ...

What is the method to incorporate type hints for my unique global Vue directives?

When I import or create a directive in an SFC file, I can receive TypeScript hints like this: import vFocus from 'my-custom-component-library'; View demo with ts hints However, if I globally register it in main.ts, the type hints are not availa ...