#typescript error when explicitly importing types

4 messages · Page 1 of 1 (latest)

feral elk
#

i'm writing a worker / pages function and am getting a typescript error when i explicitly import a type into my .ts file.

this code works fine and shows now errors:

export const onRequest: PagesFunction = async (context) => {
  return new Response( 'foobar' )
}

while this code causes a typescript error:

import type {
  PagesFunction,
} from '@cloudflare/workers-types/experimental'

export const onRequest: PagesFunction = async (context) => {
  return new Response( 'foobar' )
}

the error i'm getting is:

Type '(context: EventContext<unknown, any, Record<string, unknown>>) => Promise<Response>' is not assignable to type 'PagesFunction'.
  Type 'Promise<Response>' is not assignable to type 'Response | Promise<Response>'.
    Type 'Promise<Response>' is not assignable to type 'Promise<import(".../node_modules/@cloudflare/workers-types/experimental/index").Response>'.
      Type 'Response' is missing the following properties from type 'Response': webSocket, cf

i tried to ignore the error and delete the import type bits, but it's nagging at me and i can't let it go. 🙂

any thoughts or advice? has anyone else run into the same problem?

heady plover
#

The issue is that the type for Response is wrong, it's using typescripts default one, instead of the one that workers and pages use

#

If you import the response type too, it should be fine