Octokit pagination does not play well with Typescript

267 views Asked by At

I have the following reasonably straight-forward code:

export async function getAllRepos({
  token,
}: {
  token: string;
}): Promise<ReposListForOrgResponseData> {
  const octokit = buildOctokit(token);

  const repos = await octokit.paginate(octokit.repos.listForOrg, {
    org: OWNER,
  });

  return repos;
}

Typescript does not like this and gives me the error:

src/lib/github.ts:130:3 - error TS2322: Type '{ id: number; node_id: string; name: string; full_name: string; owner: { name?: string | null | undefined; email?: string | null | undefined; login: string; id: number; node_id: string; avatar_url: string; ... 14 more ...; starred_at?: string | undefined; }; ... 79 more ...; allow_forking?: boolean | undefined; }[]' is not assignable to type 'ReposListForOrgResponseData'.
  Type '{ id: number; node_id: string; name: string; full_name: string; owner: { name?: string | null | undefined; email?: string | null | undefined; login: string; id: number; node_id: string; avatar_url: string; ... 14 more ...; starred_at?: string | undefined; }; ... 79 more ...; allow_forking?: boolean | undefined; }' is not assignable to type '{ id: number; node_id: string; name: string; full_name: string; owner: { login: string; id: number; node_id: string; avatar_url: string; gravatar_id: string; url: string; html_url: string; followers_url: string; ... 9 more ...; site_admin: boolean; }; ... 73 more ...; license: { ...; }; }'.
    The types of 'owner.gravatar_id' are incompatible between these types.
      Type 'string | null' is not assignable to type 'string'.
        Type 'null' is not assignable to type 'string'.

130   return repos;
      ~~~~~~

I'm not sure why the types don't match up but it's impossible to figure this out following the definitions or looking at the octokit website. @octokit/rest and @octokit/types don't seem to have anything to do with each other.

Octokit/rest is at 18.12.0 and the types are at 5.5.0.

1

There are 1 answers

0
Cristian Cepeda On

The same is happening to me, I'm not TS expert but for now, I am dealing with it using Type Assertion:

import { Endpoints } from '@octokit/types'

let repos: unknow = octokit.paginate(octokit.rest.apps.listReposAccessibleToInstallation, {...})
    
repos = repos as Endpoints['GET /installation/repositories']['response']['data']['repositories']