-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
type of Octokit is incorrect #2115
Comments
I've no idea where the I agree the |
You would do that if you want to pass function myThing(ocktokit: Octokit): void
function myThing(ocktokit: undefined): void
function myThing(ocktokit: null): void
function myThing(): void
function myThing(ocktokit?: Octokit | null | undefined): void {
} ...among many more use cases. Right now we can't pass octokit around unless we assume that it's always there or cast the type manually. This also means you can't safely type a conditional initializer or memoize it. |
The problem was a plugin returning |
🎉 This issue has been resolved in version 1.0.6 🎉 The release is available on: Your semantic-release bot 📦🚀 |
The problem should not happen again once octokit/core.js#356 is merged an released. However, I couldn't figure out a way to test for Do you have an idea how to add a typescript-only check whether |
You could use the narrowing trick I used: const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN! })
if (octokit) {
octokit // should be the correct type, so `expectType` here. Should NOT be never
} But I don't know if that will work with the test suite you have set up. When I must force type checkes like this, I usually use what the DefinitelyTyped repo uses. |
Thanks! This did the trick expectType<void>(octokitWithVoidAndNonVoidPlugins); That worked before octokit/core.js#356 but is now throwing a TypeScript error 👍🏼 |
TL;DR don't union
void
if you want TypeScript to work as expected.My original findings in #2112 follow below:
@gr2m I checked your PR on @neenjaw repro and yes it solves his issue, but I did find the actually issue with the type.
The type of
Octokit
is, regardless of what Tim does in his repro:This results in your PR working because
octokit.graphql
reaches intoOctokit
as can be seen here:So far, so good, right?
The problem is that
void
that's there. This makes it so that you can't narrow this value when doing a truthy (or falsy) check, which is what Tim is running into. Let me demonstrate:This is understandable behaviour. Something that is
void
can never be truthy. We can further proof that this is what happens by inspecting the else case:The source
octokit.d.ts
has this faulty type:@neenjaw the reason this worked with earlier versions is that
void
didn't trump other types in the union, and now it does. As far as I can tell this has been the change that broke the type, but the change is correct. It shouldn't bevoid & { shape }
but just be{ shape }
.Originally posted by @SleeplessByte in #2112 (comment)
The text was updated successfully, but these errors were encountered: