Andrew Jones
Andrew Jones

Reputation: 1001

Cannot find namespace error

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.

Upvotes: 13

Views: 33065

Answers (2)

Be Kind
Be Kind

Reputation: 5172

In my universe Cannot find namespace error is CLI service thing that goes away after restarting npm watcher.

Upvotes: 2

John Weisz
John Weisz

Reputation: 31924

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.

Upvotes: 4

Related Questions