What is the best way to import a typescript file using a provided string?

I have a directory filled with JSON schemas, all coded in TypeScript.

My goal is to import them collectively while preserving the typing, instead of having to write out numerous import statements.

These schemas are utilized for validating JSON data being sent to my API using Ajv. I aim to align the endpoints of my API with the same structure as the schema names.

/users -> schemas/users.schema.ts
/account/stats -> schemas/account/stats.schema.ts

I possess an array containing various endpoint paths and would find it greatly beneficial if I could execute:

API.Mount("account/stats")

This means that essentially:

import `/schemas/${mount}.schema.ts` <-- import all these files

Answer №1

Importing with wildcards in Typescript alone seems to be unavailable, as indicated in this discussion.

A recommended approach would be to establish an index.ts file within the /schemas directory:

export { default as Stats } from './stats.schema.ts';
export { default as Users } from './users.schema.ts';
export { Items } from './items.schema.ts'; // If it's not a default export

Subsequently, when you need to utilize them in your code, simply import like so:

import { Items, Users } from '/schemas'

To delve deeper into the purpose of index.ts files, this related post may provide helpful insights.

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

Abort S3 file upload using ASW-SDK

Is there a way to abort an upload without raising an error Upload aborted. when calling upload.abort()? import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3'; import { Progress, Upload } from "@aws-sdk/lib-storage"; cons ...

Aligning arguments within the function signature

Is it possible to specify in TypeScript that the method 'foo' always expects FooData, etc. for the following code snippet: const search = (data: FooData|BarData|BazData, method:"foo"|"bar"|"baz") => { //Perform some common operations retu ...

Dealing with errors within the node.js platform

Currently, I am developing a nodejs web api that includes a function responsible for returning a user object associated with a specific authentication token: module.exports.getByToken = function (token_value, callback) { mongoose.model('tokens&ap ...

Struggling to make Mongoose with discriminator function properly

I seem to be facing an issue with my schema setup. I have defined a base/parent Schema and 3 children schemas, but I am encountering an error message that says: No overload match this call Below is the structure of my schema: import { model, Schema } fr ...

Sign up for notifications about updates to a variable within an Angular service

Is there a way to track changes in the value of a variable within an Angular service? I've been searching for a solution, but haven't been able to find one yet. In my layout's header component, I have a counter that displays the number of ...

When I incorporate Express into my React project, I encounter an error stating that the prototype

After attempting to set up a basic React project that connects to a MySQL database, I encountered an error. When requiring 'express' and rebuilding the project, I received the following message when trying to open it in the browser: "Uncaught Ty ...

Declaring a function in TypeScript triggers an error notification

I encountered a typescript error while running my code stating that "require" is not a function. To resolve this, I declared the function beforehand; however, a new typescript error now occurs saying "Modifiers cannot appear here." Can anyone assist me w ...

Using NodeJS and Moongose to efficiently assign properties from req.body in bulk

Is there a way to streamline the process of assigning req.body properties to a newly created record, especially when dealing with a model that has a large number of properties? For example: var task = new Task(); task.title = req.body.title; task.desc = ...

Transforming an array of flat data into a hierarchical tree structure

I'm facing a challenge with my script. I have an Array of FlatObj and some rules, and I need to create a converter function that transforms them into TreeObj. The Rules are: If an object has a higher depth, it should be a child of an object with a l ...

Storing the state of DevExtreme DataGrid in Angular

Currently, I have integrated the DevExtreme DataGrid widget into my Angular application. Here is a snippet of how my DataGrid is configured: <dx-data-grid id="gridContainer" [dataSource]="employees" [allowColumnReordering]="true" [allo ...

Unable to find the module... designated for one of my packages

Within my codebase, I am utilizing a specific NPM package called my-dependency-package, which contains the module lib/utils/list-utils. Moreover, I have another package named my-package that relies on my-dependency-package. When attempting to build the pr ...

Tips for preventing my component from being duplicated during the development process

I found a helpful guide on creating a JavaScript calendar in React that I am currently following. After implementing the code, I successfully have a functional calendar UI as shown below: // https://medium.com/@nitinpatel_20236/challenge-of-building-a-cal ...

Type definitions in Typescript for the style property of Animated.View

One of my components has a Props interface that extends ViewProps from React Native, like this: export interface Props extends ViewProps { // Custom props } As a result, this also extends the style prop. However, I am facing an issue while using Animat ...

Removing scrollbar from table in React using Material UI

I successfully created a basic table using react and material UI by following the instructions found at: https://material-ui.com/components/tables/#table. The table is functioning properly, but I am finding the scrollbar to be a bit inconvenient. https:// ...

Error encountered in updating Express + Angular route: PUT operation not allowed

I'm attempting to update a schema using a form, but I keep encountering the following error on both app.put and app.post methods: PUT https://myapp-demo.herokuapp.com/api/events/5523da4d97c5000300f6e713 404 (Not Found) and an ajax callback error Er ...

Embarking on a fresh node.js venture - Seeking the counterpart of angular-seed tailored for node.js

Currently searching for a seed project to launch an angular.js and node.js project. Came across a few examples: https://github.com/btford/angular-express-seed https://github.com/pilwon/ultimate-seed Any personal experiences with these? Any recommendati ...

ESLint warning: Potentially risky assignment of an undetermined data type and hazardous invocation of an undetermined data type value

Review this test code: import { isHtmlLinkDescriptor } from '@remix-run/react/links' import invariant from 'tiny-invariant' import { links } from '~/root' it('should return a rel=stylesheet', () => { const resp ...

Enhancing Code Completion Feature for Multiline Strings in Visual Studio Code

When attempting to include HTML code in a multiline string using backticks within TypeScript, I've noticed that VS Code doesn't offer auto-completion for the HTML tags. Take this example: @Component({ selector: 'app-property-binding&ap ...

function for app.get callback in express.js

Hey there, I'm a bit puzzled by the syntax of the get route function because there seem to be two versions. Here's an example of some code: First one: app.get('/users', function(req,res){ ... }); Second one: app.get('u ...

What is the best way to send multiple values from node.js to typescript?

My Node.js post API currently returns a token, but I want it to include the user's email, id, etc: app.post('/auth', function (req, response) { const body = req.body; console.log(req.body); let query = `select * from users wher ...