UNPKG

4 kBMarkdownView Raw
1# Typed Event Target
2This is the Typed Event Target library you are looking for.
3
4## Usage
5
6```ts
7import { TypedEventTarget } from "@worker-tools/typed-event-target";
8
9// Can use custom event maps:
10class Foo extends TypedEventTarget<{
11 "error": ErrorEvent
12 "custom": CustomEvent<string>
13}> {
14 /* ... */
15}
16
17// Enjoy correctly typed event here:
18const foo = new Foo()
19foo.addEventListener("error", ev => { /* ev: ErrorEvent */ })
20foo.addEventListener("custom", ev => { /* ev: CustomEvent<string> */ })
21foo.addEventListener("unknown", ev => { /* ev: Event */})
22
23// Can use built-in event maps from e.g. lib.dom.d.ts:
24class WorkerLike extends TypedEventTarget<WorkerEventMap> { /* ... */ }
25
26// Can be used as a type:
27type WindowLike = TypedEventTarget<WindowEventMap>;
28
29// Uses built-in EventTarget:
30foo instanceof EventTarget // => true
31
32// No intermediate classes:
33Object.getPrototypeOf(Foo.prototype) === EventTarget.prototype // => true
34
35// Exports helper types in case you need them:
36import type {
37 TypedEventListener,
38 TypedEventListenerObject,
39 TypedEventListenerOrEventListenerObject
40} from "@worker-tools/typed-event-target"
41```
42
43<br/>
44
45--------
46
47<br/>
48
49<p align="center"><a href="https://workers.tools"><img src="https://workers.tools/assets/img/logo.svg" width="100" height="100" /></a>
50<p align="center">This module is part of the Worker Tools collection<br/>⁕
51
52[Worker Tools](https://workers.tools) are a collection of TypeScript libraries for writing web servers in [Worker Runtimes](https://workers.js.org) such as Cloudflare Workers, Deno Deploy and Service Workers in the browser.
53
54If you liked this module, you might also like:
55
56- 🧭 [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers
57- πŸ”‹ [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support
58- πŸ“„ [__Worker HTML__][html] --- HTML templating and streaming response library
59- πŸ“¦ [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.
60- πŸ†— [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text
61- 🎏 [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...
62- πŸ₯ [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.
63- πŸ¦‘ [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.
64
65Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:
66- ✏️ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...
67- πŸ“ [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.
68- πŸ¦• [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Deno’s native HTTP server.
69
70[router]: https://workers.tools/router
71[middleware]: https://workers.tools/middleware
72[html]: https://workers.tools/html
73[kv-storage]: https://workers.tools/kv-storage
74[cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage
75[deno-kv-storage]: https://workers.tools/deno-kv-storage
76[kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill
77[response-creators]: https://workers.tools/response-creators
78[stream-response]: https://workers.tools/stream-response
79[json-fetch]: https://workers.tools/json-fetch
80[json-stream]: https://workers.tools/json-stream
81[request-cookie-store]: https://workers.tools/request-cookie-store
82[extendable-promise]: https://workers.tools/extendable-promise
83[html-rewriter]: https://workers.tools/html-rewriter
84[location-polyfill]: https://workers.tools/location-polyfill
85[deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter
86
87Fore more visit [workers.tools](https://workers.tools).