在Typescript 2.2中,我试图定义一个具有各种插件选项的模块(HapiJS)。
我将核心代码重构为多个.d.ts文件,然后使用以下模式(as seen here)从index.d.ts导入和重新导出它们:
export * from './hapi/connection';
export * from './hapi/reply';
export * from './hapi/request';
export * from './hapi/response';
export * from './hapi/route';
export * from './hapi/server_views';
export * from './hapi/server';
在一个单独的模块中,它扩展了它们的as seen here
import * as hapi from 'hapi';
declare module 'hapi' {
interface IFileHandler {
/** path - a path string or function as described above (required). */
path: string | IRequestHandler<string>;
...
}
// Extending hapi core:
interface IRouteConfiguration {
file?: string | IRequestHandler<string> | IFileHandler;
但是,当我这样做时,上面所有对IRequestHandler
的引用都会出现错误:“找不到名称‘IRequestHandler.’”。如果所有的hapi代码都被移回到一个巨大的index.d.ts中,那么它就会像预期的那样工作。有没有办法使用多个hapi定义文件来实现这一点?
发布于 2017-05-10 14:44:01
我还没有尝试过,但也许可以使用下面这样的东西(尽管我怀疑interface hapi.IRouteConfiguration {
是否可以接受):
import * as hapi from 'hapi';
declare module 'hapi' {
interface IFileHandler {
/** path - a path string or function as described above (required). */
path: string | hapi.IRequestHandler<string>;
...
}
// Extending hapi core:
interface hapi.IRouteConfiguration {
file?: string | hapi.IRequestHandler<string> | IFileHandler;';
也可以从hapi/server
导入,或者任何需要的部分。
https://stackoverflow.com/questions/43276921
复制