[英]Debugging TypeScript Node.js Compilation Error
I am trying to setup a debugging environment on an 2019 project so I added the following script to my package.json
我正在尝试在 2019 项目上设置调试环境,因此我将以下脚本添加到我的
package.json
"dev:debug": "tsc-watch --onFirstSuccess \"node --inspect -r ts-node/register src/app.ts\"",
Running it would give me the following运行它会给我以下
> node-api-starter@1.0.0 start:dev D:\p\my-project
> tsc-watch --onFirstSuccess "node --inspect -r ts-node/register src/app.ts"
7:18:10 AM - Starting compilation in watch mode...
7:18:27 AM - Found 0 errors. Watching for file changes.
Debugger listening on ws://127.0.0.1:9229/b9a130c4-473d-4b55-a512-50ae9cff15a3
For help, see: https://nodejs.org/en/docs/inspector
D:\p\my-project\node_modules\ts-node\src\index.ts:423
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/api/auth/auth.controller.ts:580:29 - error TS2339: Property 'auth' does not exist on type 'Request<ParamsDictionary>'.
580 user.refreshToken = req.auth.refreshToken;
~~~~
src/api/auth/auth.controller.ts:581:23 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary>'.
581 user.userId = req.user.id;
~~~~
src/api/auth/auth.controller.ts:617:82 - error TS2339: Property 'auth' does not exist on type 'Request<ParamsDictionary>'.
617 const user = await userService.getUserInfoByCustomColumn("refreshToken", req.auth.refreshToken);
~~~~
src/api/auth/auth.controller.ts:782:76 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary>'.
782 const user = await userService.getUserInfoByCustomColumn("userId", req.user.id, true);
~~~~
src/api/auth/auth.controller.ts:800:46 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary>'.
800 await userService.updateUserPassword(req.user.id, insertedNewPassword);
~~~~
at createTSError (D:\p\my-project\node_modules\ts-node\src\index.ts:423:12)
at reportTSError (D:\p\my-project\node_modules\ts-node\src\index.ts:427:19)
at getOutput (D:\p\my-project\node_modules\ts-node\src\index.ts:554:36)
at Object.compile (D:\p\my-project\node_modules\ts-node\src\index.ts:760:32)
at Module.m._compile (D:\p\my-project\node_modules\ts-node\src\index.ts:839:43)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (D:\p\my-project\node_modules\ts-node\src\index.ts:842:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Example of one error一个错误的例子
auth.controller.ts - Line 577
const refresh = async (req: Request, res: Response, next: NextFunction) => {
try {
const user = new User();
user.refreshToken = req.auth.refreshToken;
...
}
Request
being a part of the Express library Request
成为 Express 库的一部分
interface Request<P extends core.Params = core.ParamsDictionary> extends core.Request<P> { }
There are 2 files that declare Express
namespace and export Request
interfaces with different varibales (see below)有 2 个文件声明
Express
命名空间并使用不同的变量导出Request
接口(见下文)
request.d.ts
declare namespace Express {
export interface Request {
user?: any;
auth?: any;
}
}
declarations.d.ts
declare module 'draftjs-to-html';
declare namespace Express {
export interface Request {
availableCountries: number[];
language: string;
pagination: {
pageSize: number;
pageNumber: number;
};
}
}
So what happens here is that the first compilation using tsc-watch
is done successfully but then some errors in the project pop up.所以这里发生的情况是,使用
tsc-watch
的第一次编译成功完成,但随后项目中出现了一些错误。
ts-node
tries to recompile?ts-node
会尝试重新编译?tsc-watch
?tsc-watch
不同的配置进行编译?First, ts-node
rebuilds your sources, so you do the compilation job twice.首先,
ts-node
会重建您的源代码,因此您需要执行两次编译工作。 Not sure if the problem is caused by it, maybe ts-node
is unable to locate your tsconfig
.不确定问题是否由它引起,也许
ts-node
无法找到您的tsconfig
。
As an immediate fix I'd suggest merging the existing express.Request
declaration, whatever it is in your case, with your own.作为一个直接的解决方法,我建议将现有的
express.Request
声明(无论您的情况如何)与您自己的声明合并。 Basically, you put that declaration from request.d.ts
somewhere in your source code where it is available for your controller functions and update with proper types where needed (or roll with any
):基本上,您将
request.d.ts
中的声明放在源代码中的某个位置,它可用于您的 controller 函数,并在需要时使用适当的类型进行更新(或使用any
滚动):
declare global {
export namespace Express {
export interface Request {
user?: any;
auth?: {refreshToken: string};
}
}
}
Now Request
sports a properly typed refreshToken
.现在
Request
运动一个正确输入的refreshToken
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.