Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
  • Loading branch information
Richienb committed Sep 22, 2022
1 parent 87cb03b commit d06e4e7
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = tab

[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# Auto detect text files and perform LF normalization
* text=auto
* text=auto eol=lf
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
46 changes: 46 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type {ValueOf} from 'type-fest';

type EventListenerOrEventListenerObject<Value extends Event | CustomEvent> = (value: Value) => void | {
handleEvent(value: Value): void;
};

/**
Strictly typed definitions for [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
Only used for type-checking; compiles to no code.
@example
```
import type StrictEventTarget from 'strict-event-target';
const target = new (EventTarget as typeof StrictEventTarget<{
foo: string; // Event with data
}, [
'bar', // Event without data
]>);
target.dispatchEvent(new CustomEvent('foo', {detail: 'The'}));
target.dispatchEvent(new Event('bar'));
```
@example
```
import type StrictEventTarget from 'strict-event-target';
class Foo extends (EventTarget as typeof StrictEventTarget<{
foo: string; // Event with data
}, [
'bar', // Event without data
]>) {}
```
*/
export default class StrictEventTarget<EventsWithData extends Record<string, unknown> = {}, EventsWithoutData extends string[] = []> implements EventTarget { // eslint-disable-line @typescript-eslint/ban-types
addEventListener<Name extends keyof EventsWithData>(name: Name, callback: EventListenerOrEventListenerObject<CustomEvent<EventsWithData[Name]>> | null, options?: AddEventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types
addEventListener<Name extends ValueOf<EventsWithoutData>>(name: Name, callback: EventListenerOrEventListenerObject<Event> | null, options?: AddEventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types

dispatchEvent(event: Event | CustomEvent<ValueOf<EventsWithData>>): boolean;

removeEventListener<Name extends keyof EventsWithData>(name: Name, callback: EventListenerOrEventListenerObject<CustomEvent<EventsWithData[Name]>> | null, options?: EventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types
removeEventListener<Name extends ValueOf<EventsWithoutData>>(name: Name, callback: EventListenerOrEventListenerObject<Event> | null, options?: EventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types
}
21 changes: 21 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type StrictEventTarget from './index.js';

type EventsWithData = {
a: string;
};

type EventsWithoutData = ['b'];

const target: StrictEventTarget<EventsWithData, EventsWithoutData> = new (EventTarget as typeof StrictEventTarget<EventsWithData, EventsWithoutData>)();

function aCallback(_event: CustomEvent<string>) {} // eslint-disable-line @typescript-eslint/no-empty-function
function bCallback(_event: Event) {} // eslint-disable-line @typescript-eslint/no-empty-function

target.addEventListener('a', aCallback);
target.addEventListener('b', bCallback);

target.dispatchEvent(new CustomEvent('a', {detail: 'foo'}));
target.dispatchEvent(new Event('b'));

target.removeEventListener('a', aCallback);
target.removeEventListener('b', bCallback);
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Richie Bendall

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "strict-event-target",
"version": "0.0.0",
"description": "Strictly typed definitions for EventTarget",
"license": "MIT",
"repository": "Richienb/strict-event-target",
"author": {
"name": "Richie Bendall",
"email": "richiebendall@gmail.com"
},
"type": "module",
"types": "index.d.ts",
"engines": {
"node": ">=14"
},
"scripts": {
"test": "xo && tsd"
},
"files": [
"index.d.ts"
],
"keywords": [
"EventTarget",
"TypeScript"
],
"dependencies": {
"type-fest": "^3.0.0"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^3.0.1",
"tsd": "^0.24.1",
"typescript": "^4.8.3",
"xo": "^0.52.3"
}
}
55 changes: 55 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# strict-event-target

> Strictly typed definitions for [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
Only used for type-checking; compiles to no code.

## Install

```sh
npm install strict-event-target
```

## Usage

```ts
import type StrictEventTarget from 'strict-event-target';

const target = new (EventTarget as typeof StrictEventTarget<{
foo: string; // Event with data
}, [
'bar', // Event without data
]>);

target.dispatchEvent(new CustomEvent('foo', {detail: 'The'}));

target.dispatchEvent(new Event('bar'));
```

```ts
import type StrictEventTarget from 'strict-event-target';

class Foo extends (EventTarget as typeof StrictEventTarget<{
foo: string; // Event with data
}, [
'bar', // Event without data
]>) {}
```

## API

### StrictEventTarget<EventsWithData?, EventsWithoutData?>

#### EventsWithData

Type: `Record<string, unknown>`\
Default: `{}`

Events that will be created with [`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent), thus being able to contain custom data.

#### EventsWithoutData

Type: `string[]`\
Default: `[]`

Events that will be created with [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event).
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@sindresorhus/tsconfig"
}

0 comments on commit d06e4e7

Please sign in to comment.