"Facing an issue where ts-node is not recognizing d.ts files, despite tsc being able to compile them

I am currently using typescript along with express and attempting to enhance the request object in express.

Below is my server.ts file:

import express, { Request, Response } from "express";
const app = express();
app.use(function(req: Request, res: Response, next) {
    req.customProperty = 200;
});

And here is my extends.d.ts file:

declare namespace Express {
    export interface Request {
        customProperty: number;
    }
}

While developing, I am utilizing VSCode. The IDE does not show any warnings.

Compiling the code using tsc also works without any issues.

However, when running the code using ts-node, an error message is encountered:

src/server.ts:19:9 - error TS2339: Property 'customProperty' does not exist on type 'Request<ParamsDictionary>'.

19     req.customProperty = 200;
           ~~~~~~~~~~~

Answer №1

During the course of one of my projects, a similar situation occurred. To solve it, I decided to include the --files flag when running ts-node:

ts-node --files server.ts

For more information, you can refer to the documentation, which mentions:

If you need to use files, include, or exclude, make sure to enable the --files flag or set TS_NODE_FILES=true

Answer №2

To define the variable testVal, you can utilize the following code snippets:

Method 1:

declare global {
  namespace Express {
    interface Request {
      testVal : number
    }
  }
}

Method 2:

declare module 'express' {
  interface Request {
    testVal : number
  }
}

I trust that this will work as expected.

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

What is the best way to organize my routes with Express.Router() across different files?

I'm currently grappling with the challenge of breaking down the routes within my routes.js file into separate files. The existing setup of my routes.js file is as follows: const pullController = require('./controllers/pullController'); const ...

"The implementation of real-time data retrieval through Socket.io is currently not functioning as expected

As a beginner in mean-stack, I tried to implement real-time data following some instructions but encountered errors. Can someone guide me on how to correctly use socket.io? I have provided my code below for fetching users from mongodb, kindly review and co ...

Do I have to wait for the HTTP get request to access the fetched object property?

I am currently working with Angular and TypeScript on a dish-detail component that is accessed through 'dishes/:id' The dish object returned has a property called components, which contains an array of objects with two properties: id: type stri ...

Error TS2322: You cannot assign a Promise<any> to a string type

Having an issue in my react app where I am attempting to import the img source but encountering an error: TS2322: Type 'Promise<any>' is not assignable to type 'string'. What is the correct way to import an element into a variabl ...

Issue encountered while implementing GraphQL in an Express application using Node.js

I have encountered an issue while attempting to set up a GraphQL server in Express. The error message that I am receiving is as follows: var graphqlHTTP = require('express-graphql'); // express graphql var { buildSchema } = require('graphql ...

What is the best way to dynamically assign formControlNames in Angular using *ngFor?

I am currently facing a challenge with setting form controls using *ngFor over objects in an Array. The number of objects in the array can vary, sometimes resulting in only 1 object while other times multiple objects are present. My specific issue revolve ...

Leveraging passport.js and direct SQL queries for authentication purposes

Currently tackling a project with an issue that needs resolving. I am looking to integrate user login logic utilizing the Passport API, but struggling to grasp its functionality, especially when implementing it using plain SQL queries. While scouring thr ...

NodeJs Express: The request cannot be posted

I'm facing an issue while trying to post a request. I made sure that I have set the urlencoded option to false. Every time I attempt to post with parameters, I keep getting "Cannot GET ....." on the webpage. Uncertain about the reason behind this er ...

What is the best way to encapsulate a slider within a fragment to prevent the occurrence of the "Type 'Element[]' is not assignable to type 'ReactNode'" error?

I'm encountering an issue with a Slider component in a NextJs landing page template. Every time I try to map through an array within the Slider component, I receive an error. I've attempted to find solutions online and came across this related th ...

Searching for a specific document in a collection using Mongoose's

Need help retrieving a document from the database by its ID. The queries findOne() and find() are working fine, but findById() is returning null. What could be causing this issue? I even tried hard coding an ID with the same result. const getLocation = asy ...

Obtaining the dimensions of each individual child component within an NgTemplate

I have the following code snippet within my template. While I can iterate through its components using `get`, it does not return an object that allows me to access deeper into the HTML attributes. <ng-template #container></ng-template> Compon ...

tips for sending types as properties in a versatile component

I am facing a challenge with passing types as props to a reusable component. I have a component where I pass rows and headings as props, but I need to find a way to pass types as props as well. Below is the code for my reusable component: import { TableCe ...

separating routes using express router and transferring objects

As I work on organizing my routes, I encountered an issue where I am struggling to pass an instance of something to a specific route using Express 4 and the router. For instance, I want to pass passport to the login.js file. What is the correct way to do t ...

Adding elements to an array within an object utilizing Mongoose

Having trouble updating the posts array in my data object. Here is the schema I'm working with: const mongoose = require('mongoose'); const Post = require('./post'); const Review = require('./comment') const User = ...

Res.end isn't halting the script's execution process

I'm currently facing an issue while building an API around a third-party API in my Express route. The problem is that the script keeps executing even after encountering a 406 error. Below is the snippet of my code: app.get('/submit/:imei', a ...

Attempting to configure a webhook eventsub through the Twitch API by utilizing ngrok as the intermediary

After triggering a test event using the Twitch CLI, an error response was received indicating: Post "https://1562-5-182-32-19.ngrok.io/api/twitch/eventsub/": context deadline exceeded (Client.Timeout exceeded while awaiting headers). The notification even ...

What is the best way to dynamically insert values into a JSON object?

I'm currently working with a JSON object in Angular and I need to dynamically add values that the user enters. Despite searching extensively, I haven't found a straightforward method to achieve this. I simply want to understand how to append key- ...

Utilizing parameterized routing to direct traffic to the /userid endpoint instead of the literal /{user id}

My setup includes a router with the following routes: router.get('/', index_controller.index); router.get('/login', index_controller.login) router.get('/profile/:userId', index_controller.profile) I have two controllers that ...

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 ...

What reasons underlie the existence of various methods for importing Modules in JavaScript?

I'm confused about the distinctions when it comes to importing a JavaScript module in various ways such as: CommonJS ES5 ES6 NodeJS Typescript What is the reason for having multiple methods of importing JavaScript modules? Is the concept of a "modu ...