Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find namespace error

Tags:

typescript

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.

like image 614
Andrew Jones Avatar asked Mar 02 '16 09:03

Andrew Jones


People also ask

How do I import a namespace in typescript?

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.

Could not find module TypeScript?

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!


2 Answers

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.

like image 101
John Weisz Avatar answered Oct 21 '22 04:10

John Weisz


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

like image 24
ego Avatar answered Oct 21 '22 02:10

ego