通过使用Octokit包,我想列出所有拉取请求(client.pulls.list
)。
我有一个 GitHubClient(Octokit 的包装器)和 GitHubService(GitHubClient 的包装器)。GitHubService 有一个使用带有perPage?: number;
属性的接口的选项参数,而 GitHubClient 接受带有带有属性的接口的选项per_page?: number;
在下面的代码中,我options
在 GitHubClient 类中丢失了类型检查。
我做错了什么以及如何正确设置选项类型?
import Octokit from '@octokit/rest';
interface PaginationParams {
page?: number;
// camelcase
perPage?: number;
}
interface GitHubPaginationParams {
page?: number;
// underscored
per_page?: number;
}
class GitHubClient {
private client: Octokit;
constructor() {
this.client = new Octokit();
}
getPullRequests(options: PaginationParams) {
// lost typings of "options" with spread operator (no typescript error)
return this.client.pulls.list({ owner: 'octokit', repo: 'hello-world', state: 'open', ...options });
// this works (typescript error)
// return this.client.pulls.list({ owner: 'octokit', repo: 'hello-world', state: 'open', page: options.page, per_page: options.per_page });
// this works
// return this.client.pulls.list({ owner: 'octokit', repo: 'hello-world', state: 'open', page: options.page, per_page: options.perPage });
}
}
class GitHubService {
private ghClient: GitHubClient;
constructor() {
this.ghClient = new GitHubClient();
}
async getPullRequests(options: GitHubPaginationParams) {
return this.ghClient.getPullRequests(options);
}
}
我希望打字稿会引发错误,因为 GitHubService 的options
接口与 GitHubClient 接口不同options
。