1 | # Typed Event Target
|
2 | This is the Typed Event Target library you are looking for.
|
3 |
|
4 | ## Usage
|
5 |
|
6 | ```ts
|
7 | import { TypedEventTarget } from "@worker-tools/typed-event-target";
|
8 |
|
9 | // Can use custom event maps:
|
10 | class Foo extends TypedEventTarget<{
|
11 | "error": ErrorEvent
|
12 | "custom": CustomEvent<string>
|
13 | }> {
|
14 | /* ... */
|
15 | }
|
16 |
|
17 | // Enjoy correctly typed event here:
|
18 | const foo = new Foo()
|
19 | foo.addEventListener("error", ev => { /* ev: ErrorEvent */ })
|
20 | foo.addEventListener("custom", ev => { /* ev: CustomEvent<string> */ })
|
21 | foo.addEventListener("unknown", ev => { /* ev: Event */})
|
22 |
|
23 | // Can use built-in event maps from e.g. lib.dom.d.ts:
|
24 | class WorkerLike extends TypedEventTarget<WorkerEventMap> { /* ... */ }
|
25 |
|
26 | // Can be used as a type:
|
27 | type WindowLike = TypedEventTarget<WindowEventMap>;
|
28 |
|
29 | // Uses built-in EventTarget:
|
30 | foo instanceof EventTarget // => true
|
31 |
|
32 | // No intermediate classes:
|
33 | Object.getPrototypeOf(Foo.prototype) === EventTarget.prototype // => true
|
34 |
|
35 | // Exports helper types in case you need them:
|
36 | import 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 |
|
54 | If 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 |
|
65 | Worker 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 |
|
87 | Fore more visit [workers.tools](https://workers.tools).
|