问题 Typescript中的TypeError


这是我的问题: 我收到此错误:

未捕获的TypeError:对象原型可能只是一个Object或null:undefined

export abstract class AbstractLogicExpression {
    protected _logicChildExpressions: AbstractLogicExpression[] = Array();
    protected _precedence = 0;
    protected _parsed = false;
    protected _expressionType = "";

    protected rightAssociative = false;

    public toDNF() {
        for (let i = 0; i < this.logicChildExpressions.length; i++) {
            let actualLogicExpression: AbstractLogicExpression = this.logicChildExpressions[i];

            if (actualLogicExpression._expressionType == "~") {

                let logicConjunction = actualLogicExpression.logicChildExpressions[0];

                let var1 = logicConjunction.logicChildExpressions[0];
                let var2 = logicConjunction.logicChildExpressions[1];

                if (logicConjunction._expressionType == "*") {
                    actualLogicExpression.logicChildExpressions[0] = new LogicOr();
                    //actualLogicExpression.logicChildExpressions[0].add(new LogicNeg(var1));
                    //actualLogicExpression.logicChildExpressions[0].add(new LogicNeg(var2));
                }
            }
        }
    }
}

由于两条注释行之前的行,我收到此错误:

actualLogicExpression.logicChildExpressions[0] = new LogicOr();

我通过注释测试它并取消注释行,因为我在错误消息中没有得到行号。

有人知道我能做什么吗? 如果您需要更多代码。我可以张贴一些东西......

这里是LogicOr的代码: https://pastebin.com/T28Zjbtb


3292
2018-06-07 20:03


起源

我猜LogicOr有问题。你能发布那个类的定义吗?也许这是相关的? github.com/Microsoft/TypeScript/issues/14734 - dbandstra
如果我用null替换LogiOr,你的权利就会消失。但为什么? - leet
您是否正确导入它并确定它已正确捆绑?在我看来,这个类在运行时不再存在了? - dbandstra
那么它是一个anuglar应用程序。我正确导入LogicOr: pastebin.com/CyuxVCQR - leet


答案:


你在这一行上收到错误:

actualLogicExpression.logicChildExpressions [0] = new LogicOr();

错误消息是

未捕获的TypeError:对象原型可能只是一个Object或null:undefined

一旦熟悉了类及其工作原理,就很容易理解(https://basarat.gitbooks.io/typescript/docs/classes.html)。

错误意味着 new LogicOr 失败是因为 LogicOr 正在扩展一些东西 undefined。简单的例子:

let Bar; 
class Foo extends Bar { } // Uncaught TypeError: Object prototype may only be an Object or null: undefined

更多

修复bug LogicOr 及其继承链。


10
2018-06-08 01:07



但LogicOr从AbstractLogicExpression正确扩展。 Typescript无法处理循环依赖吗? - leet
Is Typescript unable to handle circular dependencies? 这是commonjs的限制。建议删除它们。我用的分析 alm-tools.gitbooks.io/alm/content/features/dependency.html - basarat
我通过循环依赖解决了问题,现在它可以工作了 - leet


除此之外,循环依赖的实际问题是因为在使用之前未加载的资源之一。如果您的资源无序加载,您也会收到此错误。

考虑使用gulp编译的这个示例:

// File Parent.ts
export class Parent {
    public prop: string = "prop";
}
//File Child.ts
export class Child extends Parent {
    public prop2: string = "prop2";
}

和gulp编译

gulp.task('compile-js', function () {
return gulp.src(['code/Child.js', 'code/Parent.js'])
    .pipe(sourcemaps.init())
    .pipe(concat('app.bundle.js'))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('app/build'));
});

输出文件app.bundle.js将出错“Uncaught TypeError:Object prototype可能只是一个Object或null:undefined”,因为生成的代码将首先执行Child类的创建(它依赖于Parent类)在加载父类之前。

如果您查看生成的javascript,您将获得:

var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var Child = /** @class */ (function (_super) {
    __extends(Child, _super);
    function Child() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.prop2 = "prop2";
        return _this;
    }
    return Child;
}(Parent));
var Parent = /** @class */ (function () {
    function Parent() {
        this.prop = "prop";
    }
    return Parent;
}());

当你运行这个时,你会得到:

未捕获的TypeError:对象原型可能只是一个Object或null:undefined     在setPrototypeOf()

要解决此问题,只需更改gulp文件中的资源顺序或用于准备或加载页面的javascript的任何方法。

return gulp.src(['code/Parent.js', 'code/Child.js'])

有很多方法可以解决这个问题,这只是一个帮助您了解问题以及如何解决问题的示例。无论你找到什么方法来解决这个问题,最后,错误就是要求javascript引擎做一些你在执行时尚未给出指令的东西。

希望这可以帮助, 干杯


4
2018-06-22 04:00



这个问题对我来说,谢谢! - Denny