graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

Typescript error after updating typescript to 4.7.2

Open iscekic opened this issue 2 years ago • 4 comments

const abortController = new AbortController(); // the dom one, not the npm one

client.request({
  // ...
  signal: abortController.signal,
});
Type 'AbortSignal' is not assignable to type 'AbortSignal'. Two different types with this name exist, but they are unrelated.
      Types of property 'onabort' are incompatible.
        Type '((this: AbortSignal, ev: Event) => any) | null' is not assignable to type '((this: AbortSignal, ev: Event) => any) | null'. Two different types with this name exist, but they are unrelated.
          Type '(this: AbortSignal, ev: Event) => any' is not assignable to type '(this: AbortSignal, ev: Event) => any'. Two different types with this name exist, but they are unrelated.
            The 'this' types of each signature are incompatible.
              Type 'AbortSignal' is missing the following properties from type 'AbortSignal': reason, throwIfAborted

node_modules/graphql-request/dist/types.d.ts:47:5
    47     signal?: Dom.RequestInit['signal'];
           ~~~~~~
    The expected type comes from property 'signal' which is declared here on type 'RequestOptions<Exact<{ /* ... */ }>>'

iscekic avatar May 27 '22 14:05 iscekic

I managed to work around the issue:

import { RequestInit } from "graphql-request/dist/types.dom";

client.request({
  // ...
  signal: abortController.signal as NonNullable<RequestInit["signal"]>,
});

iscekic avatar May 27 '22 14:05 iscekic

I don't know why the lib had to redefine its own AbortSignal but it feels like an anti-pattern. We should rely on the standard object otherwise it makes the usage weird and brittle. When typing as you suggested above, it doesn't accept signal from global AbortController and you have to explicitly cast it.

Nargonath avatar Jul 25 '22 10:07 Nargonath

It's a bug that this library ships with standard DOM types (from Typescript). It shouldn't import them explicitly as then those end up in compiled bundle. Or it should use import type statement but I think it should just rely on those being available without importing.

rchl avatar Aug 09 '22 12:08 rchl

It does that on purpose but I'm not convinced it's a proper solution:

https://github.com/prisma-labs/graphql-request/blob/2f221a4c5470c22dad7749b53cb1ed560727fd42/src/types.dom.ts#L1-L8

rchl avatar Aug 09 '22 12:08 rchl

Any news on this ? Casting doesn't seems like the "proper solution" even tho it works..

AzzouQ avatar Oct 11 '22 02:10 AzzouQ

Currently working on a MR that should fix this issue (#411)

marhaupe avatar Nov 08 '22 14:11 marhaupe

closed by https://github.com/jasonkuhrt/graphql-request/commit/702ef92ae382455ede353ac34300b25d31749e1c

jasonkuhrt avatar Apr 05 '23 03:04 jasonkuhrt