⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Broken Octokit Types #6

@dsnsgithub

Description

@dsnsgithub

The GitHub Feed types are not type safe, caused by broken Octokit types.

if (type === "PullRequestEvent" && (payload as any).action === "closed" && pr.merged) {
verb = "merged";
} else if (type === "PullRequestReviewEvent") {
const state = (payload as any).review.state?.toLowerCase();
if (state === "commented") continue;
verb = state.replace("_", " ");
} else if (type === "PullRequestEvent" && allowedActions.includes((payload as any).action)) {
verb = (payload as any).action;
} else continue;

I will have to fork it and update the types myself, since apparently there is no maintainer for the official Octokit JS repositories anymore.
octokit/openapi-types.ts#232 (comment)
octokit/rest.js#128

My Fix

Use my fork, add this to package.json1:

"overrides": {
    "@octokit/openapi-types": "github:dsnsgithub/openapi-types.ts#main"
},

Use this hack to typeguard:

export type EventPayloadMap = {
  CreateEvent: components["schemas"]["create-event"];
  DeleteEvent: components["schemas"]["delete-event"];
  DiscussionEvent: components["schemas"]["discussion-event"];
  IssuesEvent: components["schemas"]["issues-event"];
  IssueCommentEvent: components["schemas"]["issue-comment-event"];
  ForkEvent: components["schemas"]["fork-event"];
  GollumEvent: components["schemas"]["gollum-event"];
  MemberEvent: components["schemas"]["member-event"];
  PublicEvent: components["schemas"]["public-event"];
  PushEvent: components["schemas"]["push-event"];
  PullRequestEvent: components["schemas"]["pull-request-event"];
  PullRequestReviewCommentEvent: components["schemas"]["pull-request-review-comment-event"];
  PullRequestReviewEvent: components["schemas"]["pull-request-review-event"];
  CommitCommentEvent: components["schemas"]["commit-comment-event"];
  ReleaseEvent: components["schemas"]["release-event"];
  WatchEvent: components["schemas"]["watch-event"];
};

function isEvent<T extends keyof EventPayloadMap>(
  event: components["schemas"]["event"],
  type: T
): event is components["schemas"]["event"] & { type: T; payload: EventPayloadMap[T] } {
  return event.type === type;
}

Example Usage:

import { Octokit } from "@octokit/rest";
import type { components } from "@octokit/openapi-types";

...

const res = await octokit.rest.activity.listPublicEventsForUser({
	username: "dsnsgithub"
})

for (const event of res.data) {
	if (isEvent(event, "PullRequestEvent")) {
		// event is now PullRequestEvent and is typeguarded.
	}  else if (isEvent(event, "IssuesEvent")) {
		// event is now IssuesEvent and is typeguarded.
	}
}

Footnotes

  1. You may have to delete node_modules/package-lock.json and reset Vercel build cache for the override to take effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions