最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

express - Typescript type declaration .d.ts not working with nodemon(ts-node) - Stack Overflow

programmeradmin4浏览0评论

I want to add userName property to Request object in express using type declaration file .d.ts to be able to set it through middleware and use in other routes.

here is my /src/types/index.d.ts

export declare global {
    namespace Express {
        interface Request {
            userName?: string;
        }
    }
}

/src/index.ts

...
app.use((req: Request, res: Response, next: NextFunction) => {
    req.userName = "anyName";
    next();
});
...

tsconfig.json

{
    "compilerOptions": {
        "target": "es2016",
        "module": "commonjs",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true,
        "rootDir": "./src",
        "outDir": "./dist"
    }
}

The Problem:

  • It is working without error for
    • npx tsc && node ./dist/index.js
  • But not working with
    • nodemon ./src/index.ts or
    • npx ts-node ./src/index.ts

The Error: TSError: ⨯ Unable to compile TypeScript: src/index.ts:7:9 - error TS2339: Property 'userName' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'. 7 req.userName = "anyName";

I am looking for solution without need to add .d.ts files into tsconfig.json or without using reference directive. If any ...

I want to add userName property to Request object in express using type declaration file .d.ts to be able to set it through middleware and use in other routes.

here is my /src/types/index.d.ts

export declare global {
    namespace Express {
        interface Request {
            userName?: string;
        }
    }
}

/src/index.ts

...
app.use((req: Request, res: Response, next: NextFunction) => {
    req.userName = "anyName";
    next();
});
...

tsconfig.json

{
    "compilerOptions": {
        "target": "es2016",
        "module": "commonjs",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true,
        "rootDir": "./src",
        "outDir": "./dist"
    }
}

The Problem:

  • It is working without error for
    • npx tsc && node ./dist/index.js
  • But not working with
    • nodemon ./src/index.ts or
    • npx ts-node ./src/index.ts

The Error: TSError: ⨯ Unable to compile TypeScript: src/index.ts:7:9 - error TS2339: Property 'userName' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'. 7 req.userName = "anyName";

I am looking for solution without need to add .d.ts files into tsconfig.json or without using reference directive. If any ...

Share Improve this question asked Mar 8 at 17:13 Chirag PatelChirag Patel 381 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

To inlude and exclude files as defined in tsconfig.json at the time of starting the server, You have to use files option with ts-node as described in ts-node npmjs

use one of the following command to start the server:

npx ts-node --files ./src/index.ts

or

npx nodemon --exec "ts-node --files" ./src/index.ts

发布评论

评论列表(0)

  1. 暂无评论