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:
- Why are the global declarations in
index.d.ts
not being recognized? - Does the file name
index.d.ts
affect the recognition of global types in TypeScript? - 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:
- Why are the global declarations in
index.d.ts
not being recognized? - Does the file name
index.d.ts
affect the recognition of global types in TypeScript? - 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 badges2 Answers
Reset to default 0Since index.ts
contains actual logic so typescript treats index.d.ts
as a module instead of global declaration file. There are possible solutions:
- Rename
index.d.ts
file to something else likeglobals.d.ts
orcustom-types.d.ts
. This is the best approach as in this case typescript would not consider it as module. - Explicitly mark it as non-module that means remove
export{};
fromindex.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条)