octokit.js: Upgrading to v17 breaks TypeScript types
Hello!
Here is the error:
'Octokit' refers to a value, but is being used as a type here.
import { Octokit } from "@octokit/rest";
class MyClass {
constructor(public logger: Logger, public octokit: Octokit) {}
^--- error is here
}
I think the typings might just be pointing to the wrong thing somehow? I notice in package.json that types refers to index.d.ts but when importing the package it is grabbing the types from node_modules/@octokit/rest/dist-types/index.d.ts.
When I open that file, this is all it contains:
import { Octokit as Core } from "@octokit/core";
export declare const Octokit: (new (...args: any[]) => {
[x: string]: any;
}) & {
new (...args: any[]): {
[x: string]: any;
};
plugins: any[];
} & typeof Core & import("@octokit/core/dist-types/types").Constructor<void> & import("@octokit/core/dist-types/types").Constructor<{
paginate: import("@octokit/plugin-paginate-rest").PaginateInterface;
} & import("@octokit/plugin-rest-endpoint-methods/dist-types/generated/types").RestEndpointMethods>;
TypeScript: 3.7.5 Node: 12.15 Yarn: 1.22.0
This code worked prior to v17 and I don’t see anything in the changelog regarding changes to types.
Thanks!
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 17
- Comments: 52 (21 by maintainers)
Commits related to this issue
- build: downgrade to @octokit/rest v16 to avoid broken types We recently updated `@octokit/rest`, but the most recent version currently has broken TypeScript definitions. To workaroun this until the u... — committed to devversion/material2 by devversion 4 years ago
- build: downgrade to @octokit/rest v16 to avoid broken types We recently updated `@octokit/rest`, but the most recent version currently has broken TypeScript definitions. To workaroun this until the u... — committed to devversion/material2 by devversion 4 years ago
- build: downgrade to @octokit/rest v16 to avoid broken types We recently updated `@octokit/rest`, but the most recent version currently has broken TypeScript definitions. To workaroun this until the u... — committed to devversion/material2 by devversion 4 years ago
- build: update to angular v9.0.6 (#18785) * build: update to angular v9.0.6 Updates to Angular v9.0.6. This version contains a fix for `@angular/bazel` that should fix the release output to not l... — committed to angular/components by devversion 4 years ago
- build: downgrade to @octokit/rest v16 to avoid broken types We recently updated `@octokit/rest`, but the most recent version currently has broken TypeScript definitions. To workaroun this until the u... — committed to devversion/material2 by devversion 4 years ago
- Add release output validation for manifest paths and downgrade… (#18865) * build: add release output validation for manifest paths Ensures that https://github.com/angular/angular/commit/9581658 fi... — committed to angular/components by devversion 4 years ago
Although this works, the usage is very cryptic. The IDE will not help you. I am using the types I created, but they are not a replacement for real typings.
This is awesome in terms of “power of typescript” 🤩, Thank you for the hint! So i did this:
and then this:
But is hope we can use exported types soon 🙏 😀
What we are really missing is
Maybe this is a naive question, but if the various methods can return these types, what makes it so difficult to export them? Why does a builtin solution require type-gymnastics to extract them?
The best way in my opinion, would be to re-export the types via @octokit/rest.
Otherwise people will need to install other packages explicitly.
But this is the nature of a type’d language and is handled with proper versioning. I am trying to upgrade from @octokit/rest 16.36.0 -> 18.0.0 and it’s a total mess. I’ve got hundreds of TypeScript errors now about conflicts between types and namespaces, and I use the Octokit types (which seem to be buried in @octokit/types) like
Octokit.PullsGetResponseandOctokit.ReposListCommitsResponseItem[]and countless others, and none of them seem to be properly exposed here.Can you provide sample code for how consumers are expected to import the namespace and its types?
I need to create a list of ChecksCreateParamsOutputAnnotations that I’m going to eventually pass to checks.create(), but I can’t store them in the proper type. I don’t understand why types that are part of the API are not exported. Is there a workaround that I’m missing?
This helps a little bit to simplify extracting types (might be the wrapper that @jurijzahn8019 was talking about), but it makes it impossible to (e.g. in vscode) use F12 to get to the original definition of the type to look at its properties.
Hmm I think the right place to import endpoint response types from would be https://github.com/octokit/types.ts. It would require some work though. They currently exist in https://github.com/octokit/plugin-rest-endpoint-methods.js/blob/80132aaaa03a61d4b7645cc25a860ad6b02fd427/src/generated/types.ts#L10-L20928 but are not being exported.
For the time being you should be able to infer the type like this:
Hi, also this does not work anymore:
Neither
Octokit.TeamsListResponseItemnorTeamsListResponseItemare exported anymore.Thanks for digging into it! I might be able to spend some time next week looking into it more. I’ll report back with any findings
The might be some forensic information here for you too: https://github.com/googleapis/repo-automation-bots/pull/311
Do you use the types for anything other than pagination?
Instead of
I’ll probably implement something like this
With the types for
teamsbeing set correctlyI’ve an open pull request for
@octokit/typesthat will add two helpershttps://github.com/octokit/types.ts/pull/32
These allow to extract the response type / response data type from the endpoint methods.
Example:
I will create a separate pull request which will export a type for each response type in a separate PR. I’m not yet sure if I’ll export the response type in
@octokit/typesor@octokit/plugin-rest-endpoint-methods, or bothFeedback welcome 😁
@jurijzahn8019 Thanks! It works. 🎉
not yet, follow this issue: https://github.com/octokit/types.ts/issues/25
Hi @prokopsimek,
this could work
@bcoe this is unrleated to the original issue.
I will do two things that will help
octokit.request, just like they already exist foroctokit.endpoint.By the way you can simplify your code
What is the
keyparameter?I sat down again to try to spend a bit of time upgrading libraries that use
octokit/rest.js, and am a bit at a loss. Historically I’d been using this approach:To perform a request to an arbitrary GitHub endpoint, and provide a type hint about the response that I expect back.
With
v17.x.xI can’t find similar type hints exported anywhere.@disintegrator how about giving the exported type the same name (
Octokit), so you don’t have to do a separate import for it: