Ali Salehi
Ali Salehi

Reputation: 6999

can't have namespace and module import in one file

file1.ts

namespace test {
    export class AA {}
}

file2.ts

/// <reference path="file1.ts" />

import * as express from "express"; // without this line it works

namespace test {
    export class BB {
        constructor(a:AA){
              var r = express.Router();
              ...
        }
    }
}

If I comment the import line above, the code compiles but express import is missing. If I keep the import, I get

enter code hereerror TS2304: Cannot find name 'AA'

Any idea how to fix this?

Upvotes: 0

Views: 76

Answers (1)

TSV
TSV

Reputation: 7641

You are mixing so-called "internal" and "external" typescript modules (http://www.typescriptlang.org/Handbook#modules).

"import" keywoard tells compiler that you are using "external" approach. In this case

file1.ts - exports something

module test {
    export class AA {}
}

export = test;

file2.ts - imports your test module

import * as express from "express"; // without this line it works
import { AA } from "test"

module test {
    export class BB {
        constructor(a:AA){
              var r = express.Router();
              ...
        }
    }
}

Update 1

This works for me in VS2015:

file "test.ts":

namespace test {
    export class AA {
        prop: string;
    }
}

export = test;

file "consumer.ts":

import { AA } from "test";

namespace test {
    export class BB {
        constructor(a: AA) {
            a.prop = "some val";
        }
    }
}

Upvotes: 1

Related Questions