I have the following setup:
// enums.ts
export enum DocumentType {
Email = 0,
Unknown = 1
}
-
// remote.ts
/// <reference path="./remote.d.ts" />
import enums = require('./enums');
class Remote implements test.IRemote {
public docType: enums.DocumentType;
constructor() {
this.docType = enums.DocumentType.Unknown;
}
}
export = Remote;
-
// remote.d.ts
import * as enums from './enums';
declare module test {
export interface IRemote {
docType: enums.DocumentType;
}
}
But when I run tsc over this I get Cannot find namespace 'test'
from remotes.ts. What am I missing?
Other information that might be useful: I've recently upgraded from Typescript 1.5 to Typescript 1.8 and replaced the use of const enums with plain enums as in the example.
Use a file tsconfig. @Pavel_K In the TypeScript handbook: "To reiterate why you shouldn't try to namespace your module contents, the general idea of namespacing is to provide logical grouping of constructs and to prevent name collisions.
To solve the cannot find module 'typescript' error, make sure to install typescript globally by running the npm i -g typescript command and create a symbolic link from the globally-installed package to node_modules by running the npm link typescript command. Copied!
You need to export the internal module from remote.d.ts
as well:
remote.d.ts
import * as enums from './enums';
export declare module test {
export interface IRemote {
docType: enums.DocumentType;
}
}
This is because you have the external module remote
(the file itself is the module when there is a top-level import
or export
statement), from which types and other symbols are available when they are exported, much like how IRemote
is exported from module test
.
In other words, you have an internal module inside an external module, but the internal module is not exported. Also, the IRemote interface is effectively double wrapped, and would qualify for the fullname remote.test.IRemote
.
Note: IMO, mixing internal modules and external modules in the same project can lead to many issues and inconveniences if you are not careful and as such, should be avoided when possible.
In my universe Cannot find namespace
error is CLI service thing that goes away after restarting npm watcher.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With