I'm having a problem with import
and module
in typescript 2.4.1. I have two files:
testAdd.ts:
module mymodule {
export class myClassAdd {
static add(left: number, right: number): number {
return left + right;
}
}
}
testCallAdd.ts:
//import 'someLibrary';
module mymodule {
export class myClassCallAdd {
static callAdd(): void {
let result: number = mymodule.myClassAdd.add(3, 4); //error here
}
}
}
This code as it is now compiles fine. I've found that if I try to import some library using the import
keyword, I start to get errors. In testCallAdd.ts
, if I uncomment the import on the first line, I get an error that says myClassAdd does not exist on type typeof mymodule
I don't understand why I'm getting this error or how to fix it. The import
statement seems to be doing something to prevent the compiler from seeing testAdd.ts
. Can someone explain what went wrong and how to fix it?
This came about because I have an angular site that works fine, but I want to test it using jasmine. I can't figure out how to set it up so that both the angular piece and the unit tests have access to the code.
For your case you need to use the triple-slash directive:
/// <reference types="somelibrary" />
See: http://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
However, it would be best recommended to use module system, instead of namespace (which is what you are doing).
You can learn more about module and namespace here: http://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html
If you do intend to use namespace, since TypeScript 1.5 the syntax
module mymodule { ... }
is deprecated and replaced bynamespace mymodule { ... }
http://www.typescriptlang.org/docs/handbook/namespaces.html https://github.com/unional/typescript-guidelines/blob/master/pages/default/namespaces-and-modules.md