[Schema Inaccuracy] GET /repos/{owner}/{repo}/issues/{issue_number}/timeline" events union is not discriminating

Open
#5,801 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
openapi, typescript
Domain
api

Research direction

Start with the OpenAPI schema for GET /repos/{owner}/{repo}/issues/{issue_number}/timeline and inspect how its event response union is defined. Check how the event field is represented across variants, then verify that event values discriminate the payload so timelineEvent.created_at is available for merged events.

Written by the indexing model from the issue text.

Description

feature
What happened?

const timelinePages = octokit.paginate.iterator(
  octokit.issues.listEventsForTimeline,
  { repo, owner, issue_number },
);

(async function () {
  for await (const page of timelinePages) {
    for (const timelineEvent of page.data) {
      if (timelineEvent.event === "merged") timelineEvent.created_at;
    }
  }
})().catch((e) => console.log(e));


gives an error

Property 'created_at' does not exist on type '{ id: number; node_id: string; url: string; actor: { name?: string | null | undefined; email?: string | null | undefined; login: string; id: number; node_id: string; avatar_url: string; gravatar_id: string | null; ... 14 more ...; user_view_type?: string | undefined; }; ... 5 more ...; label: { ...; }; } | ... 20 mo...'.
Property 'created_at' does not exist on type '{ event?: string | undefined; sha: string; node_id: string; url: string; author: { date: string; email: string; name: string; }; committer: { date: string; email: string; name: string; }; message: string; tree: { ...; }; parents: { ...; }[]; verification: { ...; }; html_url: string; }'. (ts 2339)

because TypeScript doesn't seem to have a way to discriminate the values in the union of event types.

It's basically impossible to inspect any of the event payload in a typed way due to this union being overlapping.

I think the fix would be to use the event field as a discriminator.

Versions

Octokit 22.0

Relevant log output

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

The told me to reopen this issue here: https://github.com/octokit/types.ts/issues/676#issuecomment-3135915038

Dominant language
No language data
Stars
1.6k
Forks
342
Avg merge
3h 33m
Merged PRs (30d)
51

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from github/rest-api-description

All issues in github/rest-api-description

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.