typescript Intellij建议参考错误:未定义

n53p2ov0  于 2023-02-25  发布在  TypeScript
关注(0)|答案(1)|浏览(144)

在Intellij Idea中调试测试用例中的 typescript 代码时,在Evaluate窗口(Alt + F8)中出现以下错误:

ReferenceError: InternalServerException is not defined
    at eval (eval at getStatusCodeAndResponse (C:\Users\<user>\Documents\Projecten\stations-api\app\common\error-handler.ts:17:13), <anonymous>:1:1)
    at Object.getStatusCodeAndResponse (C:\Users\<user>\Documents\Projecten\stations-api\app\common\error-handler.ts:17:13)
    at Lambda.handler (C:\Users\<user>\Documents\Projecten\stations-api\app\get-guiding-lines-combinations.ts:38:20)
    at Lambda.<anonymous> (C:\Users\<user>\Documents\Projecten\stations-api\node_modules\@aws-lambda-powertools\metrics\src\Metrics.ts:277:41)
    at Lambda.<anonymous> (C:\Users\<user>\Documents\Projecten\stations-api\node_modules\@aws-lambda-powertools\logger\src\Logger.ts:337:41)
    at Lambda.<anonymous> (C:\Users\<user>\Documents\Projecten\stations-api\node_modules\@aws-lambda-powertools\tracer\src\Tracer.ts:364:33)
    at Object.<anonymous> (C:\Users\<user>\Documents\Projecten\stations-api\app\get-guiding-lines-combinations.test.ts:41:41)
    at Promise.then.completed (C:\Users\<user>\Documents\Projecten\stations-api\node_modules\jest-circus\build\utils.js:391:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (C:\Users\<user>\Documents\Projecten\stations-api\node_modules\jest-circus\build\utils.js:316:10)

导入自定义类的方法:

import {
    BadRequestException,
    Exception,
    InternalServerException,
    NotFoundException,
} from '../models/Exceptions';

自定义类本身:

export class InternalServerException implements Error {
    message: string;
    name: string = 'InternalServer';
    statusCode: number = 500;

    constructor(message?: string) {
        this.message = message ?? 'Internal server exception';
    }

    createResponse(): APIGatewayProxyStructuredResultV2 {
        return {
            statusCode: this.statusCode,
            body: this.message,
        };
    }
}

当运行测试时,它可以很容易地找到我的自定义类等。我如何让我的类在评估窗口中被识别?因为这使得调试变得容易得多。(我使用的版本是Intellij Idea 2022.3.2最终版)

9ceoxa92

9ceoxa921#

在调试过程中评估导入模块(在您的情况下为InternalServerException)时出现ReferenceError异常,原因是导入对象的转换方式怪异+源Map中缺少名称Map:如果变量在翻译/模糊处理时被重命名,并且没有提供适当的名称Map,则调试器将无法将源代码中的变量与VM中的变量匹配。
例如,import somefunction from './somefunc'通常编译为

Object.defineProperty(exports, "__esModule", { value: true });
var somefunc_1 = require("./somefunc");
somefunc_1.default();

并且没有生成名称Map(生成的sourcemap中为"names":[]),调试器无法将.ts文件中的变量与运行时的代码匹配,显示为undefined
可以关联的票证:https://github.com/microsoft/TypeScript/issues/9627https://github.com/webpack/webpack/issues/3957https://github.com/babel/babel/issues/1468

相关问题