TypeError:对象原型只能是Object或为null:未定义 [英] TypeError: Object prototype may only be an Object or null: undefined

查看:1834
本文介绍了TypeError:对象原型只能是Object或为null:未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,如果我导入Entity,则会收到帖子的主题错误(TypeError:对象原型只能是一个Object或null:未定义),但是如果用实际的Entity声明替换导入,则代码可以正常运行.

Below if I import Entity I get the posts's subject error (TypeError: Object prototype may only be an Object or null: undefined), but if I replace the import with the actual Entity declaration the code runs fine.

此处的Stackblitz演示.

这是Customer.ts的形式,当我使用ts-node运行代码时会产生错误:

This is Customer.ts in the form that produces the error when I run the code with ts-node:

index.ts

export { Customer } from "./Customer";
export { Entity } from "./Entity";

Customer.ts

Customer.ts

import { Entity } from "./index";

export class Customer extends Entity {
  sku: string;
  constructor(po: any) {
    super();
    this.sku = po.sku;
  }
}

Entity.ts

Entity.ts

export abstract class Entity {
  id?: string;
}    

Run.ts(测试代码)

Run.ts (The test code)

import {Customer} from "./";

let c = new Customer({
  name: "Bob"
});
console.log(c);

如果我用这样的声明替换Entity导入:

If I replace the Entity import with the declaration like this:

export abstract class Entity {
  id?: string;
}    

export class Customer extends Entity {
  sku: string;
  constructor(po: any) {
    super();
    this.sku = po.sku;
  }
}

然后Run.ts记录以下内容:

Customer { sku: undefined }

换句话说,它运行正常并且不会产生任何错误.有想法吗?

In other words it runs fine and produces no errors. Thoughts?

推荐答案

我怀疑您的原始程序具有循环导入. Run.ts导入index.ts,后者又导入Customer.ts,又再次导入index.ts.由于index.ts已经在加载过程中,并且它本身依赖于Customer.ts,因此import { Entity } from "./index";只是将index.tsEntity(尚未设置)绑定到Customer.tsEntity,即使index.ts尚未完成加载,执行仍会继续.然后,在尝试扩展Entity时未定义.您可能会认为循环导入应该是错误的,或者JavaScript引擎应该使用其他可以正确处理您的情况的算法;我没有资格评论为什么选择当前设计. (其他人可以随意添加有关此信息.)

As I suspected, your original program has circular imports. Run.ts imports index.ts, which imports Customer.ts, which imports index.ts again. Since index.ts is already in the process of loading and itself depends on Customer.ts, the import { Entity } from "./index"; just binds the Entity of index.ts (which is not set yet) to the Entity of Customer.ts, and execution proceeds even though index.ts isn't finished loading. Then Entity is undefined at the time you try to extend it. You might argue that a circular import should be an error or that JavaScript engines should use some other algorithm that correctly handles your scenario; I'm not qualified to comment on why the current design was chosen. (Others feel free to add information about this.)

如您所见,将Customer.ts更改为直接从./Entity而不是./index导入会破坏循环,并且一切都会按预期进行.另一种解决方案是颠倒index.ts中的导入顺序.

As you saw, changing Customer.ts to import from ./Entity directly instead of ./index breaks the cycle, and everything works as expected. Another solution would be to reverse the order of imports in index.ts.

这篇关于TypeError:对象原型只能是Object或为null:未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆