Reputation: 81
New to TypeScript (but not to OO Design) I don't understand what happens
file 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)
Any Idea ?
Upvotes: 4
Views: 1773
Reputation: 81
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
Upvotes: 2
Reputation: 6706
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). 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
}
Upvotes: 1
Reputation: 16586
I don't believe imports are hoisted. Try moving import { REPOSITORY } from "./repository"
up in your code.
Upvotes: 0