【发布时间】:2019-11-27 05:43:54
【问题描述】:
通过使用 Octokit 包,我想列出所有拉取请求 (client.pulls.list)。
我有一个 GitHubClient(Octokit 的包装器)和 GitHubService(GitHubClient 的包装器)。
GitHubService 有一个 options 参数,使用带有 perPage?: number; 属性的接口,而 GitHubClient 接受带有属性 per_page?: number; 的接口的选项
在下面的代码中,我在 GitHubClient 类中丢失了 options 的类型检查。
我做错了什么以及如何正确设置选项类型?
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);
}
}
我希望 typescript 会引发错误,因为 GitHubService 的 options 接口与 GitHubClient 接口中的 options 不同。
【问题讨论】:
-
只是为了确定:你看过
@octokit/rest的分页API吗? octokit.github.io/rest.js/#pagination -
@Gregor 是的,我有,但我们正在使用 Pulls API octokit.github.io/rest.js/#octokit-routes-pulls
标签: javascript typescript api github octokit