workers-types icon indicating copy to clipboard operation
workers-types copied to clipboard

Export type definitions

Open youfoundKim opened this issue 3 years ago • 3 comments

Would it be possible to export the type definitions in index.d.ts? That would make it possible to use the types in JavaScript projects.

Here's an example of how PageFunction could be used:

import { PagesFunction } from '@cloudflare/workers-types';

/** @type {PagesFunction} */
export async function onRequest(context) {
}

It's also possible to import directly in the comment such as SvelteKit does it.

/** @type {import('@sveltejs/kit').RequestHandler} */
export async function get({ params }) {

}

youfoundKim avatar Feb 10 '22 14:02 youfoundKim

I think we should do this. There's no timeline on this just yet, but we'll do it.

threepointone avatar Mar 06 '22 06:03 threepointone

+1, having to configure this via tsconfig compiler options means disabling the default behavior of pulling in all @types packages which means it's a breaking change on a large project.

coderanger avatar Jun 09 '22 20:06 coderanger

This should be added. I'm working on a framework for workers, wkrk and I need to define the types for request and context.

For now, I'm using the global ExecutionContext, but I can't do the same with Request because it has the same name than the native Request object, so it doesn't consider the one defined in this project. Here's the specific issue that I'm working on for more details.

giovannibenussi avatar Jun 17 '22 00:06 giovannibenussi

For anyone coming across this issue, I've released a (hopefully temporary) package as a workaround:

cloudflare-workers-types-esm

Mostly just using the TS library to copy the @cloudflare/workers-types programmatically and add export clauses to them, coupled with some Github Actions black magic to semi-automatically keep the package in sync with @cloudflare/workers-types.

It's been wildly useful in my projects so far, so I plan to keep it active until this issue is resolved.

thatmattlove avatar Sep 27 '22 16:09 thatmattlove

Being forced to use ambient modules makes it cumbersome for projects having different pieces running in different contexts, that's it, stuff like the browser's Request object being polluted with CF-specific properties.

Having the option to explicitly importing types solves this issue while being just mildly annoying.

stefanmaric avatar Oct 03 '22 09:10 stefanmaric

Hey! 👋 Thanks for reporting this, we're actively thinking about ways of fixing this in the next major version of workers-types using the new type generation system. 👍

mrbbot avatar Oct 21 '22 13:10 mrbbot

Hey! 👋 As of @cloudflare/workers-types@4, you can now import type { ... } from "@cloudflare/workers-types". 🎉

mrbbot avatar Jan 19 '23 15:01 mrbbot