英文:
index.d.ts type definitions being ignored by tsc when compiling
问题
以下是您要翻译的内容:
我有一个使用TypeScript构建的Express应用程序,我尝试使用tsc
CLI工具进行编译。
然而,我面临的问题是,tsc
似乎忽略了我创建的index.d.ts
文件,并且无法改变Express Request
对象。
这是我的index.d.ts
文件:
declare global{
namespace Express{
export interface Request{
foo: string;
}
}
}
这使我可以在我的控制器请求中执行以下操作,而不会使TypeScript报告不存在
错误:
export const example = async (req: Request) => {
const { foo } = req;
// 输出“bar”。这在开发中完全正常工作。
console.log(foo);
};
我正在运行以下命令来构建我的应用程序:
tsc ./Main.ts --outdir build
这导致在使用它的每个控制器中多次出现以下错误:
error TS2339: Property 'foo' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
英文:
I have an Express app built in TypeScript that I'm attempting to compile using the tsc
CLI tool.
An issue that I'm facing, however, is that tsc
seems to ignore the index.d.ts
file that I've created and used to mutate the Express Request
object.
This is my index.d.ts
file:
declare global{
namespace Express{
export interface Request{
foo: string;
}
}
}
This allows me to do stuff like this inside my controller's requests without TypeScript spitting out a does not exist
error:
export const example = async (req: Request) => {
const { foo } = req;
// Outputs "bar". This works completely fine in development.
console.log(foo);
};
I'm running the following command to build my app:
tsc ./Main.ts --outdir build
Which results in the following error multiple times across every controller that uses it:
error TS2339: Property 'foo' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
答案1
得分: 1
尝试在你的index.d.ts文件中添加一个空的导出:
export {};
declare global{
namespace Express{
export interface Request{
foo: string;
}
}
}
英文:
Try adding an empty export to your index.d.ts file:
export {};
declare global{
namespace Express{
export interface Request{
foo: string;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论