[英]TypeError: Object prototype may only be an Object or null: undefined
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.下面如果我导入Entity
,我会得到帖子的主题错误(类型错误:Object 原型可能只是一个 Object 或 null:未定义),但如果我用实际的Entity
声明替换导入,代码运行正常。
Stackblitz demo here . Stackblitz 演示在这里。
This is Customer.ts
in the form that produces the error when I run the code with ts-node
:这是在我使用ts-node
运行代码时产生错误的形式的Customer.ts
:
index.ts索引.ts
export { Customer } from "./Customer";
export { Entity } from "./Entity";
Customer.ts客户.ts
import { Entity } from "./index";
export class Customer extends Entity {
sku: string;
constructor(po: any) {
super();
this.sku = po.sku;
}
}
Entity.ts实体.ts
export abstract class Entity {
id?: string;
}
Run.ts (The test code) Run.ts(测试代码)
import {Customer} from "./";
let c = new Customer({
name: "Bob"
});
console.log(c);
If I replace the Entity
import with the declaration like this:如果我用这样的声明替换Entity
导入:
export abstract class Entity {
id?: string;
}
export class Customer extends Entity {
sku: string;
constructor(po: any) {
super();
this.sku = po.sku;
}
}
Then Run.ts
logs this:然后Run.ts
记录这个:
Customer { sku: undefined }
In other words it runs fine and produces no errors.换句话说,它运行良好并且不会产生任何错误。 Thoughts?想法?
As I suspected, your original program has circular imports. 我怀疑,你的原始程序有循环导入。 Run.ts
imports index.ts
, which imports Customer.ts
, which imports index.ts
again. Run.ts
导入index.ts
,它导入Customer.ts
,再次导入index.ts
。 Since index.ts
is already in the process of loading and itself depends on Customer.ts
, the import { Entity } from "./index";
由于index.ts
已经在加载过程中,并且它本身依赖于Customer.ts
,因此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. 只是将index.ts
的Entity
(尚未设置)绑定到Customer.ts
的Entity
,即使index.ts
没有完成加载,执行index.ts
继续。 Then Entity
is undefined at the time you try to extend it. 然后,当您尝试扩展Entity
未定义。 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; 你可能会认为循环导入应该是一个错误,或者JavaScript引擎应该使用一些正确处理你的场景的其他算法; I'm not qualified to comment on why the current design was chosen. 我没有资格评论为什么选择当前的设计。 (Others feel free to add information about this.) (其他人可以随意添加相关信息。)
As you saw, changing Customer.ts
to import from ./Entity
directly instead of ./index
breaks the cycle, and everything works as expected. 如您所见,将Customer.ts
更改为直接从./Entity
导入而不是./index
会中断循环,一切都按预期工作。 Another solution would be to reverse the order of imports in index.ts
. 另一种解决方案是在index.ts
反转导入index.ts
。
This is not directly an answer to the example above, but I got the same error message when I had Vue and Parcel, and this kind of code:这不是对上面示例的直接回答,但是当我有 Vue 和 Parcel 时,我得到了相同的错误消息,以及这种代码:
class Proxy {}
class MyProxy extends Proxy {}
After some lengthy debugging, it turned out that there seems to be some class name conflict with the name "Proxy", maybe from Parcel.经过长时间的调试,结果发现似乎有一些 class 名称与名称“Proxy”冲突,可能来自 Parcel。 After I renamed the super class as "AbstractProxy" or anything else than "Proxy", it started to work.在我将 super class 重命名为“AbstractProxy”或“Proxy”以外的任何其他名称后,它开始工作了。
你可以试试这个命令并检查应用程序:
ng update @angular/cli @angular/core --force
npm install
ng serve -o
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.