typescript 找不到命名空间错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35743376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find namespace error
提问by Andrew Jones
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?
但是当我运行 tsc 时,我Cannot find namespace 'test'
从 remotes.ts得到。我错过了什么?
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.
其他可能有用的信息:我最近从 Typescript 1.5 升级到 Typescript 1.8,并在示例中用普通枚举替换了 const 枚举的使用。
回答by John Weisz
You need to export the internal module from remote.d.ts
as well:
您还需要从中导出内部模块remote.d.ts
:
remote.d.ts
远程.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
.
这是因为您有外部模块remote
(当有顶层import
或export
语句时,文件本身就是模块),从中导出类型和其他符号是可用的,就像如何IRemote
从 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
.
换句话说,您在外部模块中有一个内部模块,但内部模块没有导出。此外, IRemote 接口实际上是双重包装的,并且有资格使用 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.
注意:IMO,如果您不小心,在同一个项目中混合内部模块和外部模块会导致许多问题和不便,因此应尽可能避免。
回答by ego
In my universe Cannot find namespace
error is CLI service thing that goes away after restarting npm watcher.
在我的 UniverseCannot find namespace
错误中,CLI 服务在重新启动 npm watcher 后消失。