嗯,事实是这样的@types\bull
并没有真正声明一个名称空间。
嗯,确实如此,但只是将相关类型的列表分组并将它们作为默认导出一起导出,所以,它真正导出的是contents命名空间的名称,而不是名称空间本身。这就是为什么你可以导入Queue
,并使用Queue
and not Bull.Queue
,这是你应该做的,如果Queue
真正属于一个名称空间。
另外,我不知道你正在使用什么版本的 TypeScript,但你不应该使用export (...)
and export = (...)
在同一个文件中。此外,当你添加export
到一个文件,它变成一个模块的声明文件,所以,最后,你有一个默认导出命名空间的模块,然后你可以导入Queues
from myLib
, 但不是Job
, as Job
没有出现在文件中的任何位置,因此不会导出。
为了能够合并不同文件中的命名空间,您不能使用导入或导出,而只能使用声明,因为两个模块永远不能向同一个命名空间提供名称。通过使用export
,你将你的文件变成模块,一旦你这样做,它们中的命名空间就不再属于全局范围,所以即使它们具有相同的名称,它们实际上属于它们自己的模块的范围,而不是合并。
为了做你想做的事,你必须:
bull:
declare const Bull: {
(queueName: string, opts?: any): Bull.Queue;
// something like above
};
declare namespace Bull {
interface Queue {}
interface Job {}
// some other non-exported interfaces
}
myLib:
declare namespace Bull {
export interface Queues {}
}
declare interface myInterface {
foo: Bull.Queue | Bull.Queues
}
现在您真正拥有了一个包含两个声明内容的命名空间:
test.ts:
const a: Bull.Queues = {};
const g: Bull.Queue = {};
const b: Bull.Job = {};
这样它就可以工作,但不幸的是,这不是你所拥有的。你应该定义myLib
as:
import * as Bull from './bull';
export interface Queues {};
export interface myInterface {
foo: Bull.Queue | Queues;
}
然后你可以使用:
import * as Bull from './bull';
import { Queues, myInterface } from './myLib';
const a: Queues = {};
const g: Bull.Queue = {};
const b: Bull.Job = {};
const instance: myInterface = null;
或者,如果您愿意,
import * as Bull from './bull';
import * as ExtendedBull from './myLib';
const a: ExtendedBull.Queues = {};
const g: Bull.Queue = {};
const b: Bull.Job = {};
const instance: ExtendedBull.myInterface;
但是,无论如何,您都需要从两者导入bull
and myLib
.