octokit / octokit.js

The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation is unclear on Typescript usage of this package

alper opened this issue · comments

Describe the need

I can't find any documentation here about what the Typescript return types are of Octokit.

Say here:

export async function createBranch(
  repo: string,
  {
    token,
    ref,
    sha,
  }: {
    token: string;
    ref: string;
    sha: string;
  }
): Promise<any> {
  const octokit = buildOctokit(token);

  const res = await octokit.rest.git.createRef({
    owner: OWNER,
    repo,
    ref,
    sha,
  });

  return res.data;
}

It's unclear to me how to type the return type.

Is it true that the project does not supply any and we need to use something like this?
https://www.npmjs.com/package/@octokit/plugin-rest-endpoint-methods

It seems weird that this is a typescript package without type support.

SDK Version

No response

API Version

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

I face similar issue. When I check, it shows that the request accepts a RequestParameters, but I can not import it from octokit because it does not export that.

I tried to Google as well as searching across Github's Octokit's issues, but still can not find a good answer. Why it's so hard for a TypeScript project?

I have to test it but for the createBranch function I now have this which seems to work as far as ts is concerned:

import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

Promise<RestEndpointMethodTypes["git"]["createRef"]["response"]>

If you don't want to dig into the internals of Octokit, or just want a simple solution, you can simply use the ReturnType utility built into typescript.

type OctokitReturn = ReturnType<Octokit["rest"]["git"]["createRef"]>

use @octokit/types: https://github.com/octokit/types.ts#examples

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

type listUserReposParameters =
  Endpoints["GET /repos/{owner}/{repo}"]["parameters"];
type listUserReposResponse = Endpoints["GET /repos/{owner}/{repo}"]["response"];

async function listRepos(
  options: listUserReposParameters,
): listUserReposResponse["data"] {
  // ...
}