1

我在 typescript 中有一个库,可以将一个类编译为 javascript:

ClassName = ClassName_1 = class ClassName{
...
}

ClassName = ClassName_1 = decorate([
...])

当我使用依赖于该库的 Angular 前端进行编译时,这会出现错误:

./node_modules/.bin/ng build --configuration=development --base-href /client/ --deploy-url /client/

给出以下错误:

ERROR in ./node_modules/library/ClassName.js
Module build failed: Error: Debug Failure. False expression.

我用打字稿编译所有东西,2.9.2因为它是一个 Angular 6 项目。我尝试7.0.0-rc使用 `3.0.3 但我仍然有这个错误,但我可能在这里有一个假阴性。

如果我手动更改 .js 文件,它可以工作:

ClassName = class ClassName{
...
}
ClassName_1 = ClassName

ClassName = ClassName_1 = decorate([
...])
ClassName_1 = ClassName

出现此错误的代码是:

/**
 * Endpoint gives the baseDomain of MultiChat and gives the appropriate protocol.
 * This is useful so you can pass through the same object to resources and streams types.
 *
 * A custom object is introduced to allow to pass the same object to use for WebSockets and https calls.
 * Paths are also added to easily construct full routes.
 *
 * The design is to return full new objects so that you do not mix references.
 */
@injectable()
export class Endpoint {

    public secure: boolean;
    public baseDomain: string;
    public paths: string[];
    public port: number;
    public queries: Map<string, string>;

    /**
     * init initializes the Endpoint with the correct baseDomain.
     * @param secure is true if the _endpoint is reachable by HTTPS and WSS
     * @param baseDomain the basedomain of the _endpoint, eg. independer.nl.
     * @param paths additional paths to the _endpoint without slashes, eg. ["api"]
     * @param port the port the _endpoint is hosted on
     */
    constructor(secure: boolean, baseDomain: string, paths: string[] = [], port?: number) {
        this.baseDomain = baseDomain;
        this.paths = paths;
        this.port = port;
        this.secure = secure;
        this.queries = new Map<string, string>();
    }

    /**
     * addPath returns a new Endpoint object with the paths added and returns a clone of the Endpoint.
     * @param paths
     */
    public addPath(paths: string[]): Endpoint {
        const result = this.cloneEndpoint();

        result.paths = this.paths.concat(paths);

        return result;

    }

    /**
     * addQuery adds a new query parameter and returns a clone of the Endpoint.
     * @param key The key of the parameter.
     * @param value The value of the parameter.
     */
    public addQuery(key: string, value: string): Endpoint {
        const result = this.cloneEndpoint();

        result.queries.set(key, value);

        return result;
    }

    /**
     * getHTTP returns the HTTP baseDomain string.
     * If the baseDomain is secure it returns HTTPS.
     */
    public getHTTP(): string {
        const protocol = (this.secure ? "https" : "http");

        return this.getEndpoint(protocol);
    }

    /**
     * getWS returns the WebSocket baseDomain string.
     * * If the baseDomain is secure it returns WSS.
     */
    public getWS(): string {
        const protocol = (this.secure ? "wss" : "ws");

        return this.getEndpoint(protocol);
    }

    private cloneEndpoint(): Endpoint {
        return new Endpoint(this.secure, this.baseDomain, this.paths, this.port);
    }

    private getEndpoint(protocol: string) {
        const port = (this.port ? ":" + this.port : "");

        let endpoint = protocol + "://" + this.baseDomain + port;

        endpoint += this.getPath();

        if (this.queries.size) {
            endpoint += "?";

            let first = true;
            for (const key of this.queries.keys()) {
                if (!first) {
                    endpoint += "&";
                }

                endpoint += key + "=" + encodeURIComponent(this.queries.get(key));

                first = false;
            }
        }

        return  endpoint;
    }

    private getPath(): string {
        let fullPath = "";

        for (const path of this.paths) {
            fullPath += "/" + path;
        }

        return fullPath;
    }

}
4

1 回答 1

0

我用 TypeScript 2.9.2 重现了这个错误。堆栈跟踪:

Error: Debug Failure. False expression.
    at Object.getJSDocTags (.../typescript/lib/tsc.js:10302:22)
    at getJSDocTypeParameterDeclarations (.../typescript/lib/tsc.js:9069:30)
    at Object.getEffectiveTypeParameterDeclarations (.../typescript/lib/tsc.js:9065:67)
    at checkClassLikeDeclaration (.../typescript/lib/tsc.js:40190:36)
    at checkClassExpression (.../typescript/lib/tsc.js:40166:13)
    at checkExpressionWorker (.../typescript/lib/tsc.js:37693:28)
    at checkExpression (.../typescript/lib/tsc.js:37629:42)
    at checkBinaryLikeExpression (.../typescript/lib/tsc.js:37246:29)
    at checkBinaryExpression (.../typescript/lib/tsc.js:37238:20)
    at checkExpressionWorker (.../typescript/lib/tsc.js:37717:28)

我没有看到现有问题,但我没有收到 TypeScript 3.0.3 或 3.1.3 的错误。我建议您再次尝试 3.0.3 并尝试确认该版本正在实际使用中。(一种简单的方法是unknown在代码中粘贴一个位置,看看它是否被识别。)

另一种解决方案是阻止 TypeScript 加载有问题的 JavaScript 文件。我不熟悉 Angular 构建系统以了解您的所有选项,但可能的一件事是.d.ts为您的库打开声明文件 () 生成,并确保生成的.d.ts文件与.js文件一起出现您构建应用程序,因此 TypeScript 将加载.d.ts文件而不是.js文件。这也可能会为您提供出色的类型信息。

于 2018-10-12T13:46:57.183 回答