Typescript Feathers类型脚本-类型化上下文

Typescript Feathers类型脚本-类型化上下文,typescript,feathersjs,Typescript,Feathersjs,我的目标: 能够执行app.get([此处显示键入的选项]) 能够执行app.service([此处显示键入的选项])->当 使用HookContex 为了解决这个问题,我尝试了以下方法: 在declarations.d.ts文件中: imports.... import config from '@config/default.json'; declarations... // A mapping of service names to types. Will be extende

我的目标:

  • 能够执行app.get([此处显示键入的选项])
  • 能够执行
    app.service([此处显示键入的选项])
    ->当 使用HookContex
为了解决这个问题,我尝试了以下方法:

在declarations.d.ts文件中:

imports....
  import config from '@config/default.json';
 declarations...

// A mapping of service names to types. Will be extended in service files.
export interface EntityServices {
    users: Service<UserInterface>;
    subscribers: Service<SubscriberInterface>;
    permissions: Service<PermissionInterface>;
}

type Config = typeof config;

interface ModifiedConfig extends Config {
    sequelizeClient: Sequelize.Sequelize;
    'view engine': 'ejs';
}

export interface ServiceTypes extends EntityServices {}

type initialApplication = ExpressFeathers<ServiceTypes>;

type AppGet = <T extends keyof ModifiedConfig>(path: T) => ModifiedConfig[T];
type AppSet = <T extends keyof ModifiedConfig>(
    path: T,
    config: ModifiedConfig[T],
) => initialApplication;

// The application instance type that will be used everywhere else
export interface Application extends Omit<initialApplication, 'get' | 'set'> {
    get: AppGet;
    set: AppSet;
}

export type Paths = keyof ServiceTypes;
export type EntityPaths = keyof EntityServices;

export interface APIHookContext extends FeathersHookContext {
    app: Application;
    path: Paths;
}

export interface HookContext extends FeathersHookContext {}
为此:

export const sendNewUserEmail = async (context: APIHookContext) => { [some code...] return context; };
我会打字,但以下几点会让我大吃一惊:

service.hooks(hooks)调用,现在投诉APIHookContext和HookContext的应用程序和路径对象不匹配

通过深入检查,我在feathers类型中看到,HookContext指的是“this”,我认为这是未初始化的应用程序。因此,我的对象在“this”为空时填充

我进一步尝试提取
service.hooks()
函数并更新其类型,但随后问题出现在feathers导入中,例如“feathers hooks common”函数。它们传递的HookContext与APIHookContext不匹配

service.hooks函数的类型错误示例:

...type '(((context: HookContext<any, Service<any>>) => Promise<HookContext<any, Service<any>>>) | ((context: APIHookContext) => Promise<...>))[]' is not assignable to type 'Hook<any, Service<any>>[]'. Type '((context: HookContext<any, Service<any>>) => Promise<HookContext<any, Service<any>>>) | ((context: APIHookContext) => Promise<...>)' is not assignable to type 'Hook<any, Service<any>>'. Type '(context: APIHookContext) => Promise<APIHookContext>' is not assignable to type 'Hook<any, Service<any>>'. Types of parameters 'context' and 'hook' are incompatible. Type 'HookContext<any, Service<any>>' is not assignable to type 'APIHookContext'. Types of property 'app' are incompatible. Type 'Application<{}>' is missing the following properties from type 'Application': request, response, init, defaultConfiguration, and 40 more.ts(2345)
…类型“((上下文:HookContext)=>Promise)|((上下文:APIHookContext)=>Promise))[]”不可分配给类型“Hook[]”。类型“((上下文:HookContext)=>Promise)|((上下文:APIHookContext)=>Promise)”不可分配给类型“Hook”。类型“(上下文:APIHookContext)=>Promise”不可分配给类型“Hook”。参数“context”和“hook”的类型不兼容。类型“HookContext”不可分配给类型“APIHookContext”。属性“app”的类型不兼容。类型“Application”缺少类型“Application”中的以下属性:请求、响应、init、defaultConfiguration和40多个.ts(2345)
我希望在钩子方法、类方法等内部使用一致的应用程序对象。。。它包含所有的服务及其类型和事物,例如类型化的get对象等

我已经能够在declaration.d.ts文件中实现这一点,但是上面的冲突和对“this”的引用阻止了我

让我知道什么是可能的,如果有一种方法,以达到这一水平的打字


关于

我可能误解了一些东西,但是对于feathers的明确类型的类型声明有一个泛型钩子上下文,其中一个类型参数默认为any。如果这是您正在使用的类型,您可能需要指定一个类型参数。@AluanHaddad感谢您的评论,您可以提供一个示例吗?当然可以,但首先您能否确认您正在使用的包
@types/featherjs
,它是最新版本,请告诉我,我对
FeathersHookContext
是从该包导入的类型的别名的猜测是否正确?@AluanHaddad我使用的是最新版本的feathers:@feathersjs/feathers:“^4.5.3”它内置了TS。导入{HookContext as FeathersHookContext,}从'@feathersjs/feathers',让我知道上述内容是否足够。谢谢。这正是我要问的
...type '(((context: HookContext<any, Service<any>>) => Promise<HookContext<any, Service<any>>>) | ((context: APIHookContext) => Promise<...>))[]' is not assignable to type 'Hook<any, Service<any>>[]'. Type '((context: HookContext<any, Service<any>>) => Promise<HookContext<any, Service<any>>>) | ((context: APIHookContext) => Promise<...>)' is not assignable to type 'Hook<any, Service<any>>'. Type '(context: APIHookContext) => Promise<APIHookContext>' is not assignable to type 'Hook<any, Service<any>>'. Types of parameters 'context' and 'hook' are incompatible. Type 'HookContext<any, Service<any>>' is not assignable to type 'APIHookContext'. Types of property 'app' are incompatible. Type 'Application<{}>' is missing the following properties from type 'Application': request, response, init, defaultConfiguration, and 40 more.ts(2345)