我有一个非常小的节点类型记录项目,其结构如下:

在我的index.ts文件中导入"diff“模块时,如下所示:
import * as diff from 'diff';原子类型记录突然失去了定位"fgtApp.Interfaces“命名空间的能力:

一旦删除导入语句,节点就能够解决"fgtApp.Interfaces“命名空间-不像这样:

这是原子类型记录中的错误,还是我对导入这些外部模块工作方式的理解中的错误?
发布于 2015-09-12 16:34:26
这不是原子类型记录的具体问题。原子类型的语言插件使用官方的TypeScript语言服务(与Visual相同),所以这在任何编辑器中都是一个问题。
问题是,一旦指定了导入或导出,文件就变成一个模块(“外部模块”预类型记录1.5)。这意味着当import * as diff from 'diff';出现时,index.ts中声明的内容只会变成本地的,而不会考虑/合并到全局命名空间中。
TypeScript规范,第11.1节:包含至少一个外部导入声明、导出赋值或顶级导出声明的 ...source文件被视为单独的外部模块。外部模块中声明的实体仅适用于该模块,但导出的实体可以使用导入声明导入到其他模块中。
当您不使用外部模块时,TypeScript允许不同文件中的名称空间相互构建。一旦开始使用外部模块,如果不使用解决方案,就不再是这种情况了。在这种情况下,最好只是切换到使用外部模块-如果您正在做一个Node项目,这是特别容易的,因为您不必担心捆绑。
而不是像C#和.NET这样的“深度命名空间”(如C#和.NET)--开始考虑将每个TypeScript源文件作为自己的模块。如果您确实想要一个层次结构,请用文件夹结构实现一个层次结构。
即使在noImplicitAny活动的情况下,这段代码也会像您预期的那样工作:
interfaces.d.ts
// Notice that this is a d.ts file. Since it will only contain interfaces,
// making it a d.ts file means TypeScript doesn't have to worry about
// emitting it and you also can't accidentally put executable code here.
export interface IFgtService {
authenticateDisable: boolean;
failedAttempt: boolean;
authenticate: (username: string, password: string) => boolean;
}
export interface IAnotherInterfaceAsAnExample {
isCool: boolean;
}service.ts
// since there is no "relative path", diff will come from node_modules.
import * as diff from 'diff';
// since there IS a relative path, interfaces will come from ./interfaces.d.ts
import * as interfaces from './interfaces';
// You can still use namespaces inside an "external module", but mainly they
// serve as a convenient way to bundle stuff for a one-line export (see
// the last line of this file).
namespace Service {
export class FgtService implements interfaces.IFgtService {
authenticateDisable = true;
failedAttempt = true;
authenticate = (username: string, password: string) => {
let d = diff.d;
return true;
}
}
}
export = Service;index.ts
import {FgtService} from './Service';
const myService = new FgtService();
console.log(myService.authenticateDisable);https://stackoverflow.com/questions/32531405
复制相似问题