[英]TypeScript error Cannot read property 'REPOSITORY' of undefined
New to TypeScript (but not to OO Design) I don't understand what happens TypeScript的新手(但不是OO Design)我不知道会发生什么
file application.ts 文件application.ts
class
APPLICATION{
constructor(){
console.log("constructor APPLICATION")
this.database = new REPOSITORY
}
database: REPOSITORY
}
new APPLICATION
import { REPOSITORY } from "./repository"
file repository.ts 文件库
export
class
REPOSITORY {
constructor() {
console.log("constructor de REPOSITORY")
}
}
ANd I get the error 我收到错误
this.database = new repository_1.REPOSITORY;
^
TypeError: Cannot read property 'REPOSITORY' of undefined at new APPLICATION (Z:\\Documents\\Phi\\Developpement\\TypeScript\\test\\application.js:6:41) TypeError:无法在新APPLICATION上读取未定义的属性“ REPOSITORY”(Z:\\ Documents \\ Phi \\ Developpement \\ TypeScript \\ test \\ application.js:6:41)
Any Idea ? 任何想法 ?
You are perfectly right ! 你是完全正确的! I thought that the compiler was in two-passes and that the order of these statements were not imposed.
我认为编译器是两次通过的,这些语句的顺序没有强加。 As I think that this import/export mecanism should be automatic, I would prefer to hide it at the end of the code !
因为我认为这种导入/导出机制应该是自动的,所以我希望在代码末尾隐藏它! Too bad !
太糟糕了 !
Thank you 谢谢
Your import
statement for REPOSITORY
occurs after REPOSITORY
is used in the constructor for APPLICATION
, which means that it is not yet defined in the constructor (the variable assignment resulting from the import statement is not hoisted). 您在
REPOSITORY
import
语句在APPLICATION
的构造函数中使用REPOSITORY
之后发生,这意味着它尚未在构造函数中定义(未悬挂import语句产生的变量赋值)。 You will need to import prior to use: 您需要先导入才能使用:
import { REPOSITORY } from "./repository"
class APPLICATION {
constructor(){
console.log("constructor APPLICATION")
this.database = new REPOSITORY();
}
database: REPOSITORY
}
I don't believe imports are hoisted. 我不认为进口会被吊起。 Try moving
import { REPOSITORY } from "./repository"
up in your code. 尝试在代码中
import { REPOSITORY } from "./repository"
向上import { REPOSITORY } from "./repository"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.