octokit/plugin-paginate-rest.js

Help with Pagination Types for listReposAccessibleToInstallation

ajschmidt8 opened this issue · 3 comments

@gr2m, first off, thank you for all the work you do with Octokit. It's really incredible.

I'm relatively new to TypeScript and I'm having some issues with the pagination types. I'm trying to use a try/catch statement to provide some error handling with my octokit call, but I want to preserve the type information with my let declaration. I'm getting the TypeScript error shown in the dropdown below. Do you know what I'm doing wrong here?

TypeScript Error
Type 'AppsListReposAccessibleToInstallationResponseData & { id: number; node_id: string; name: string; full_name: string; owner: { login: string; ... 16 more ...; site_admin: boolean; }; ... 74 more ...; network_count: number; }[]' is not assignable to type '{ id: number; node_id: string; name: string; full_name: string; license: { key: string; name: string; url: string | null; spdx_id: string | null; node_id: string; html_url?: string | undefined; } | null; ... 81 more ...; starred_at?: string | undefined; }[] | undefined'.
  Type 'AppsListReposAccessibleToInstallationResponseData & { id: number; node_id: string; name: string; full_name: string; owner: { login: string; ... 16 more ...; site_admin: boolean; }; ... 74 more ...; network_count: number; }[]' is not assignable to type '{ id: number; node_id: string; name: string; full_name: string; license: { key: string; name: string; url: string | null; spdx_id: string | null; node_id: string; html_url?: string | undefined; } | null; ... 81 more ...; starred_at?: string | undefined; }[]'.
    The types returned by 'pop()' are incompatible between these types.
      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; }; ... 74 more ...; network_count: number; } | undefined' is not assignable to type '{ id: number; node_id: string; name: string; full_name: string; license: { key: string; name: string; url: string | null; spdx_id: string | null; node_id: string; html_url?: string | undefined; } | null; ... 81 more ...; starred_at?: string | undefined; } | undefined'.
        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; }; ... 74 more ...; network_count: number; }' is missing the following properties from type '{ id: number; node_id: string; name: string; full_name: string; license: { key: string; name: string; url: string | null; spdx_id: string | null; node_id: string; html_url?: string | undefined; } | null; ... 81 more ...; starred_at?: string | undefined; }': license, forks, open_issues, watchers

Example Screenshot:

image

Example Text:

import { Octokit } from "@octokit/rest";
import { Endpoints } from "@octokit/types";

const client = new Octokit();

(async () => {
  let repositories: Endpoints["GET /installation/repositories"]["response"]["data"]["repositories"];

  try {
    repositories = await client.paginate(
      client.apps.listReposAccessibleToInstallation
    );
  } catch (error) {
    // error handling
  }

  // Iterate over repos
  for (let i = 0; i < repositories.length; i++) {
    //
  }
})();
gr2m commented

Thank you for the kind words AJ!

Could you please share what versions of the octokit packages you are using (on mac/linux: npm ls | grep octokit)

We upgraded to much better Types today, but I've only run into a few regressions, it's like that you run into one here as well. I'm having a look

gr2m commented

I think I found the problem. @octokit/types will give you the new version with extended types, @octokit/rest gets its version trough its dependency on @octokit/plugin-rest-endpoint-methods, which has not yet been updated in @octokit/rest. That should be easy enough to fix

What I would do is to change your code slightly so you don't need to import @octokit/types separately

import { Octokit } from "@octokit/rest";

const client = new Octokit();

(async () => {
  try {
    const repositories = await client.paginate(
      client.apps.listReposAccessibleToInstallation
    );

    // Iterate over repos
    for (let i = 0; i < repositories.length; i++) {
      //
    }
  } catch (error) {
    // error handling
  }
})();

Thanks, Gregor! That looks like it works. Here are my dependency versions for reference. It's likely that the upgrade was what affected my code since this issue only appeared after updating to the latest version today. Appreciate the help here. I'll close this issue and keep an eye out for the @octokit/rest update.

Working:

{
  "dependencies": {
    "@octokit/auth-app": "^2.9.0",
    "@octokit/plugin-throttling": "^3.3.2",
    "@octokit/rest": "^18.0.6",
    "@octokit/webhooks": "^7.15.1",
  },
  "devDependencies": {
    "@octokit/types": "^5.5.0",
  }
}

Not working:

{
  "dependencies": {
    "@octokit/auth-app": "^2.10.3",
    "@octokit/plugin-throttling": "^3.3.4",
    "@octokit/rest": "^18.0.9",
    "@octokit/webhooks": "^7.15.1",
  },
  "devDependencies": {
    "@octokit/types": "^6.0.1",
  }
}