Skip to content
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

tsc fails when using octokit #2112

Closed
3 of 7 tasks
neenjaw opened this issue Jun 5, 2021 · 12 comments
Closed
3 of 7 tasks

tsc fails when using octokit #2112

neenjaw opened this issue Jun 5, 2021 · 12 comments
Labels
Status: Needs Info Full requirements are not yet known, so implementation should not be started Type: Support Any questions, information, or general needs around the SDK or GitHub APIs typescript Relevant to TypeScript users only

Comments

@neenjaw
Copy link

neenjaw commented Jun 5, 2021

Please avoid duplicates

Reproducible test case

https://www.typescriptlang.org/play?target=6&jsx=0&module=1&strict=true#code/JYWwDg9gTgLgBAbzgeQMYwga2PAvnAMyghDgCIJ0scyAoWgUwA9JY4BDAZwE8A7VQgFd+MYBF6EGMVAAsAFAEpEtOHFTjO8ACrIA0gFEAcnAC8cAOTN24ADYM4GTA17mVajfEqOcpuLwYA7ihU2DBySOyCMDIAXHA6Bsa4Cm7qvJqIcAAm7DDscPhm7AHsPl7UMAB0AOZQ7GAAjjZybqoABq2qcA2CDFDccGCCNjYASgw9DJqccgAkEAH+UHEAyjBQwLzVAIQANHCzUAyQq+ubO-uzvIIgcQCSvPBmAKwADJc27ABGDDaccQBtNYbLbbAC6SgQnS6cCOkE4OGg3DkCyWcXmiz6+141gY6LhEEh0Jhg2GYwmvWmck+Pz+6Jpv04+wIwCgmnR1xA+00uSmcWQAAUjESSaK-BAslNlGKxZyflBiTLRDA7IqxYIoDY1TDcNqCmrdaLDV02rtOlCZai+nFzP5nAArYrmM0ygk2u28R0BVw6twpNxHGAaiQ5PK0XBAA

Please select the environment(s) that are relevant to your bug report

  • TypeScript
  • Enterprise
  • Browsers
  • Node
  • Deno

Version

├── [email protected]

What happened?

When compiling with tsc v4.3.2, I receive a type error which I don't see with earlier versions (e.g. 4.1.3), nor do I see it on the TS playground example. The error:

src/lib/gh.ts:33:21 - error TS2339: Property 'graphql' does not exist on type 'never'.

33   } = await octokit.graphql(
                       ~~~~~~~

I am able to resolve this by using the @octokit/graphql and using the graphql api directly, e.g.:

import { graphql } from '@octokit/graphql'

// ...

const graphqlWithAuth = graphql.defaults({
    headers: {
      authorization: process.env.GITHUB_TOKEN,
    },
  })

const { data } = await graphqlWithAuth(
  `
    query here
  `
)

Not sure if the wrong type is being exported?

Would you be interested in contributing a fix?

  • yes
@neenjaw neenjaw added the Type: Bug Something isn't working as documented label Jun 5, 2021
@gr2m
Copy link
Contributor

gr2m commented Jun 7, 2021

It works in VS Code. It's most likely a problem with the playground, can you check in with them?

image

@gr2m gr2m closed this as completed Jun 7, 2021
@neenjaw
Copy link
Author

neenjaw commented Jun 7, 2021

My issue is not that the auto complete doesn't work. My issue is that I get a tsc error that the type doesn't exist when I am compiling it on the command line.

@gr2m
Copy link
Contributor

gr2m commented Jun 7, 2021

can you please create a minimal test repository to reproduce the problem? It is probably related to your tsconfig settings or and/or typescript version

@gr2m gr2m reopened this Jun 7, 2021
@gr2m gr2m changed the title octokit not exporting proper TypeScript type for octokit instance tsc fails when using octokit Jun 7, 2021
@gr2m gr2m added Type: Support Any questions, information, or general needs around the SDK or GitHub APIs typescript Relevant to TypeScript users only and removed Type: Bug Something isn't working as documented labels Jun 7, 2021
@SleeplessByte
Copy link

The typescript playground is actually telling us why it's failing inside the playground.

Error messages showing a specific type package cannot be resolved.

@neenjaw can you check in your VSCode if you see the same issue/similar issue? Ping me on slack again if you want to work on a repro for Gregor.

@gr2m
Copy link
Contributor

gr2m commented Jun 7, 2021

The typescript playground is actually telling us why it's failing inside the playground.

that's great, but it's still a problem with the playground that I cannot reproduce locally.

@neenjaw
Copy link
Author

neenjaw commented Jun 7, 2021

Yea, I can make one tonight, will post here with it

@gr2m gr2m added the Status: Needs Info Full requirements are not yet known, so implementation should not be started label Jun 7, 2021
@neenjaw
Copy link
Author

neenjaw commented Jun 8, 2021

Minimal test repo here: https://github.com/neenjaw/octocat-minimal-issue

can recreate tsc error locally using npm install && npm run build

@gr2m
Copy link
Contributor

gr2m commented Jun 8, 2021

It wasn't a problem with Octokit, but with the structure of your code. This PR fixes it:
neenjaw/octocat-minimal-issue#1

Does that work for you?

@neenjaw
Copy link
Author

neenjaw commented Jun 8, 2021

It would, can you explain how the previous way caused the tsc error so I can learn from it? I would have thought the if statement after the ternary would have narrowed the type appropriately, or at least excluded the never case?

@gr2m
Copy link
Contributor

gr2m commented Jun 8, 2021

I wish I could explain it in detail, but I still don't fully understand the inner working of TypeScript myself. And I think there were some changes in type narrowing as part of v4.3.

The TypeScript community is very helpful, and there are some very skilled folks looking for TypeScript questions on StackOverflow, if you'd like to ask someone more skilled than myself.

I'd also recommend reading https://www.typescriptlang.org/docs/handbook/2/narrowing.html if you haven't yet.

@gr2m gr2m closed this as completed Jun 8, 2021
@SleeplessByte
Copy link

@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:

Octokit 
  & void 
  & { paginate: PaginateInterface } 
  & Api

This results in your PR working because octokit.graphql reaches into Octokit as can be seen here:

Autocomplete of type information when typing octokit with the Octokit type showing the grapql property, among others

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:

Showing that a thruthy check narrows the type to never

const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN! })

if (octokit) {
  octokit.graphql // nope
}

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:

Showing that the falsy case has the expected thruthy type

if (octokit) {
} else {
  octokit.graphql // yep!
}

The source octokit.d.ts has this faulty type:

export declare const Octokit: typeof OctokitCore & import("@octokit/core/dist-types/types").Constructor<void & {
    paginate: import("@octokit/plugin-paginate-rest").PaginateInterface;
} & import("@octokit/plugin-rest-endpoint-methods/dist-types/types").Api>;

@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 be void & { shape } but just be { shape }.

@gr2m
Copy link
Contributor

gr2m commented Jun 8, 2021

can you please open a separate issue for this @SleeplessByte?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Info Full requirements are not yet known, so implementation should not be started Type: Support Any questions, information, or general needs around the SDK or GitHub APIs typescript Relevant to TypeScript users only
Projects
None yet
Development

No branches or pull requests

3 participants