简体   繁体   中英

How do you include a Typescript namespace in a test file?

I have a Foo.ts file that contains the namespace Foo { ... } and some exported functions. I want to test the functions in this namespace using a test file right next to this one called Foo.test.ts .

///<reference path="./Foo.ts" />
import './Foo';
console.log(typeof Foo)

However, when I try to run the test file using mocha, I get an error.

 ReferenceError: Foo is not defined

How do I include the typescript namespace so I can access Foo.bar() and other exported functions?

It looks like there is pretty sufficient documentation on namespaces here: Typescript Namespaces

Your Foo.ts should look like this:

export namespace Foo {
 export function Bar() {
      return 'Bar';
 }
}

And then in the other file:

import * as Foo from './Foo';
console.log(typeof Foo)

I think you could do this to:

import { Foo } from './Foo';
console.log(typeof Foo)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM