-
Notifications
You must be signed in to change notification settings - Fork 89
feat: add ModuleWorker
type definitions
#102
Conversation
myKVNamespace.get("foo", {type: "stream"}); | ||
|
||
interface Env extends ModuleWorker.Bindings { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicitly defining all bindings that the worker
(below) will have access to.
It extends the ModuleWorker.Bindings
interface for strictness within the generic itself, but also to prevent a user from accidentally declaring an invalid value type – like FOO: number
} | ||
|
||
const worker: ModuleWorker<Env> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define the ModuleWorker
, passing thru the Env
interface as the generic's type argument.
Doing this allows for strict type checks (and auto-complete) for all env
parameter usage throughout the worker.
Not shown is the ability to define handler functions individually while still maintaining strict typing. This is because each handler definition is, itself, a generic: // src/handlers.ts
export const fetcher: ModuleWorker.FetchHandler<Env> = async function (req, env, ctx) {
// the `env` is strictly typed
return new Response('OK');
}
// src/index.ts
import { fetcher } from './handlers';
export default { fetch: fetcher }; |
Fixes #76 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, lovely to get this in now. Thanks @lukeed! :)
Hey this is great! What needs to happen to get this merged? |
Auto-Generated Types Types are now automatically generated with releases by parsing the runtime's source code, and merging in overrides/docs defined in this repository (for generics, overloads, comments, etc). webworker no longer needs to (and shouldn't) be included in tsconfig lib. The final merged AST that's used to render the TypeScript types, workers.json, is also included in the repository. This could be used to generate bindings for other languages that compile to WebAssembly. Rust output is coming soon. Closes: #55, #75, #76, #81, #84, #96, #97, #100, #101, #102, #105, #106, #107, #108
#112 has now been merged, which includes an workers-types/overrides/events.d.ts Line 26 in 61c5989
|
When can we get this one merged? |
@maraisr You can use the |
Yup it's the same, can alias it & bring the strict types too if you want type Bindings = Record<string, KVNamespace | DurableObjectNamespace | string>;
type ModuleWorker<Env extends Bindings = Bindings> = ExportedHandler<Env>; |
Just a shame, the ergonomics about importing and defining a In any case, glad there's been efforts in this space! |
We're currently missing typedefs for creating module workers, so I included some that allow for strict-mode TS usage.
The
ModuleWorker
generic optionally accepts a typedBindings
interface, which will propagate to anyfetch
/scheduled
handlers on the definition.Included a strictly-typed module worker in the
test.ts
file, which makes use of the generic.And then here's an example module worker, without any specific
env
typing, that is completely valid too: