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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:42:14  来源:igfitidea点击:

TypeError: Object prototype may only be an Object or null: undefined

javascriptnode.jstypescriptts-node

提问by Ole

Below if I import EntityI 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 Entitydeclaration the code runs fine.

下面如果我导入Entity我得到帖子的主题错误(类型错误:对象原型可能只是一个对象或空值:未定义),但如果我用实际Entity声明替换导入,代码运行良好。

Stackblitz demo here.

Stackblitz 演示在这里

This is Customer.tsin 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 Entityimport 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.tslogs 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.tsimports index.ts, which imports Customer.ts, which imports index.tsagain. Since index.tsis already in the process of loading and itself depends on Customer.ts, the import { Entity } from "./index";just binds the Entityof index.ts(which is not set yet) to the Entityof Customer.ts, and execution proceeds even though index.tsisn't finished loading. Then Entityis 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.tsindex.ts再次进口。由于index.ts已经在加载过程中并且它本身依赖于Customer.ts,因此它import { Entity } from "./index";只是将Entityof index.ts(尚未设置)绑定到Entityof Customer.ts,即使index.ts尚未完成加载,也会继续执行。然后Entity在您尝试扩展它时未定义。你可能会争辩说循环导入应该是一个错误,或者 JavaScript 引擎应该使用其他一些正确处理你的场景的算法;我没有资格评论为什么选择当前设计。(其他人可以随意添加有关此的信息。)

As you saw, changing Customer.tsto import from ./Entitydirectly instead of ./indexbreaks 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:

您可以尝试使用此命令并检查应用程序:

  1. ng update @angular/cli @angular/core --force
  2. npm install
  3. ng serve -o
  1. ng update @angular/cli @angular/core --force
  2. npm install
  3. ng serve -o