首页 > 解决方案 > 如何为整个目录中的打字稿模块文件创建接口?

问题描述

我有api目录,我想为此目录中的模块文件指定此接口。:

export interface API<
  Model extends BaseModel,
  Filters extends Record<string, unknown>,
  Permissions extends PermissionsType<string> = PermissionsType<string>
> {
  create?: (data: Model) => HttpClientResponse<Model>
  destroy?: (id: Pick<Model, 'id'>) => HttpClientResponse
  update?: (data: Model) => HttpClientResponse<Model>
  useList?: (filters?: Filters) => ApiHookResult<PaginationData<Model>>
  useItem?: (id: Pick<Model, 'id'>) => ApiHookResult<Model>
  usePermissions?: () => ApiHookResult<Permissions>
}

这样api目录中的每个文件都只能包含这些功能。我该怎么做?

标签: typescriptmoduleinterface

解决方案


尚不完全清楚您的要求是什么,但如果您想为API每个 api 分配接口,只需导入API每个文件并使用implements.

fooApi.ts:

import { API } from 'types.ts' // or wherever you declare this type

class FooApi implements API {
  ...
}

barApi.ts:

import { API } from 'types.ts' // or wherever you declare this type

class BarApi implements API {
  ...
}

推荐阅读