Adam Keenan
Adam Keenan

Reputation: 784

Cannot find name of module in same directory

The code compiles and runs, but I'm getting errors with the type checking which blows up with a lot of files and variables. Here's an example.

Test1.ts

import Test2 = require('./Test2');

class Test1 {
    test2: Test2;
    constructor() {
        this.test2 = new Test2();
    }
}

console.log(new Test1());

Test2.ts

export = class Test2 {
    prop: number;
    constructor() {
        this.prop = 5;
    }
}

Running tsc --module commonjs Test1.ts gives me this error:

Test1.ts(4,12): error TS2304: Cannot find name 'Test2'.

And running the code outputs:

Test1 { test2: Test2 { prop: 5 } }

What am I doing wrong here?

Upvotes: 3

Views: 8202

Answers (1)

Amid
Amid

Reputation: 22382

Do not use export= / import= syntax. Its better to do it like this:

Test1.ts

import {Test2} from './Test2';

class Test1 
{
    test2: Test2;
    constructor() {
        this.test2 = new Test2();
    }
}

console.log(new Test1());

Test2.ts

export class Test2 
{
    prop: number;
    constructor() 
    {
        this.prop = 5;
    }
}

Upvotes: 4

Related Questions