javascript - Why are my global declarations in index.d.ts not being recognized in my TypeScript project? - Stack Overflow

I'm working with a TypeScript project where I have two files:index.ts - This file contains the mai

I'm working with a TypeScript project where I have two files:

  • index.ts - This file contains the main application logic.
  • index.d.ts - This file is where I declare global types.

In my index.d.ts, I'm declaring globals like this:

declare global {
  namespace Express {
    interface Request {
      user_id?: number;
      role?: string;
    }
  }
}

export {};

However, it seems that the global declarations are not being recognized in my project. If I rename index.d.ts to any other name (e.g., globals.d.ts), the global declarations work perfectly fine.

My Questions:

  1. Why are the global declarations in index.d.ts not being recognized?
  2. Does the file name index.d.ts affect the recognition of global types in TypeScript?
  3. How can I fix this issue so that my global types in index.d.ts are properly recognized?

I've ensured that index.d.ts is included in the tsconfig.json file under the include array, but the issue persists.

Things I've already tried:

  • Renamed the file to other names (e.g., globals.d.ts) and it works fine.

Any help would be appreciated!


This should help others understand your issue and offer suggestions for resolving it!

I'm working with a TypeScript project where I have two files:

  • index.ts - This file contains the main application logic.
  • index.d.ts - This file is where I declare global types.

In my index.d.ts, I'm declaring globals like this:

declare global {
  namespace Express {
    interface Request {
      user_id?: number;
      role?: string;
    }
  }
}

export {};

However, it seems that the global declarations are not being recognized in my project. If I rename index.d.ts to any other name (e.g., globals.d.ts), the global declarations work perfectly fine.

My Questions:

  1. Why are the global declarations in index.d.ts not being recognized?
  2. Does the file name index.d.ts affect the recognition of global types in TypeScript?
  3. How can I fix this issue so that my global types in index.d.ts are properly recognized?

I've ensured that index.d.ts is included in the tsconfig.json file under the include array, but the issue persists.

Things I've already tried:

  • Renamed the file to other names (e.g., globals.d.ts) and it works fine.

Any help would be appreciated!


This should help others understand your issue and offer suggestions for resolving it!

Share Improve this question asked Mar 8 at 18:13 Naga mani kanta manamNaga mani kanta manam 414 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Since index.ts contains actual logic so typescript treats index.d.ts as a module instead of global declaration file. There are possible solutions:

  1. Rename index.d.ts file to something else like globals.d.ts or custom-types.d.ts. This is the best approach as in this case typescript would not consider it as module.
  2. Explicitly mark it as non-module that means remove export{}; from index.d.ts as it is turning it into module.
declare global {
  namespace Express {
    interface Request {
      user_id?: number;
      role?: string;
    }
  }
}

Why bother writing your own declaration files? Why not declare a new Request interface that extends Express's Request interface?

Something like this: ts playground

This way you can let the typescript compiler generate the declaration file for you (using either "declaration"="true" in the tsconfig file or with the --declaration flag at the tsc CLI; link).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744889334a4599308.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信