typescript 类型错误:对象原型可能只是一个对象或 null:未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53122751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: Object prototype may only be an Object or null: undefined
提问by Ole
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
我得到帖子的主题错误(类型错误:对象原型可能只是一个对象或空值:未定义),但如果我用实际Entity
声明替换导入,代码运行良好。
This is Customer.ts
in the form that produces the error when I run the code with ts-node
:
这是Customer.ts
在我运行代码时产生错误的形式ts-node
:
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?
换句话说,它运行良好并且不会产生任何错误。想法?
回答by Matt McCutchen
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.)
正如我怀疑的那样,您的原始程序具有循环导入。 Run.ts
进口index.ts
,进口Customer.ts
,index.ts
再次进口。由于index.ts
已经在加载过程中并且它本身依赖于Customer.ts
,因此它import { Entity } from "./index";
只是将Entity
of index.ts
(尚未设置)绑定到Entity
of Customer.ts
,即使index.ts
尚未完成加载,也会继续执行。然后Entity
在您尝试扩展它时未定义。你可能会争辩说循环导入应该是一个错误,或者 JavaScript 引擎应该使用其他一些正确处理你的场景的算法;我没有资格评论为什么选择当前设计。(其他人可以随意添加有关此的信息。)
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
.
如您所见,更改Customer.ts
为./Entity
直接导入而不是./index
打破循环,一切都按预期进行。另一种解决方案是颠倒index.ts
.
回答by Kaushik Vaghani
You can try this command and check the application:
您可以尝试使用此命令并检查应用程序:
ng update @angular/cli @angular/core --force
npm install
ng serve -o
ng update @angular/cli @angular/core --force
npm install
ng serve -o