Jim
Jim

Reputation: 4182

Typescript Error Putting Classes in Separate Files

I feel like this is a really basic mistake I'm making, but I can't find the solution anywhere. I have two classes, Moo.ts:

module namespace {
export class Moo{
    constructor() {
        //   window.console.log("hello from Moo");
        var foo:Foo = new Foo();
    }
}

and Foo.ts:

 module namespace {
export class Foo{
    constructor() {
        //   window.console.log("hello from Foo");
    }
}

When running Moo.ts I would expect it to make an instance of Foo, but it doesn't. It give this error:

TypeError: namespace.Foo is not a constructor

It works fine when I put the two classes in the same ts file, but when I break them up everything falls apart. Am I missing something?

Upvotes: 1

Views: 148

Answers (1)

gilamran
gilamran

Reputation: 7304

You probably included the Foo.js (The generated file) in your html after Moo.js, this means that when Moo.js run (As it was the first js file) it did not find namespace.Foo

The order of including the generated js files is important. That's why you'd better use the tsconfig.json and include one generated js file in your html (You can use source-maps to debug the TypeScript code)

Upvotes: 1

Related Questions