Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running type tests on CI #93

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
}
],
"scripts": {
"typescript:check": "yarn tsc --noEmit",
"typescript:check": "tsc --noEmit",
"typescript:watch": "tsc --watch",
"typescript:build": "tsc",
"typescript:test": "tsc ./test/type-tests/** --noEmit",
"prettier:check": "prettier --debug-check $npm_package_config_prettier_target",
"prettier:write": "prettier --write $npm_package_config_prettier_target",
"validate": "yarn typescript:check && yarn prettier:check",
"test": "yarn jest",
"validate": "yarn typescript:check && yarn typescript:test && yarn prettier:check",
"test": "tsc ./test/type-tests/** --noEmit",
"size:check": "size-limit",
"build:clean": "rimraf dist",
"build": "yarn build:clean && yarn typescript:build",
Expand All @@ -47,7 +48,7 @@
"devDependencies": {
"@size-limit/preset-small-lib": "^7.0.5",
"@types/jest": "^27.4.0",
"expect-type": "^0.13.0",
"expect-type": "^0.15.0",
"jest": "^27.4.7",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
Expand Down
9 changes: 3 additions & 6 deletions src/bind-all.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Binding, InferEvent, InferEventType, Listener, UnbindFn } from './types';
import { Binding, InferEventType, Listener, UnbindFn } from './types';
import { bind } from './bind';

function toOptions(value?: boolean | AddEventListenerOptions): AddEventListenerOptions | undefined {
Expand Down Expand Up @@ -46,11 +46,8 @@ export function bindAll<
type: TTypes[K] | (string & {});
listener: Listener<
TTarget,
InferEvent<
TTarget,
// `& string` "cast" is not needed since TS 4.7 (but the repo is using TS 4.6 atm)
TTypes[K] & string
>
// `& string` "cast" is not needed since TS 4.7 (but the repo is using TS 4.6 atm)
TTypes[K] & string
>;
options?: boolean | AddEventListenerOptions;
};
Expand Down
2 changes: 1 addition & 1 deletion src/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function bind<TTarget extends EventTarget, TType extends InferEventType<T
// however, doing that today breaks autocompletion
// this is being fixed by https://github.com/microsoft/TypeScript/pull/51770 but we need wait for its release in TS 5.0
type: TType | (string & {});
listener: Listener<TTarget, InferEvent<TTarget, TType>>;
listener: Listener<TTarget, TType>;
options?: boolean | AddEventListenerOptions;
},
): UnbindFn {
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ type ListenerObject<TEvent extends Event> = {
};

// event listeners can be an object or a function
export type Listener<TTarget extends EventTarget, TEvent extends Event> =
| ListenerObject<TEvent>
| { (this: TTarget, ev: TEvent): void };
export type Listener<TTarget extends EventTarget, TType extends string = string> =
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Andarist I am keen for your thoughts here. I

moved Listener back to taking a string rather than an Event in order to maintain the previous type signature. Autocomplete seems to be continuing to work great.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change the only breaking change would be the type signature of bindAll

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or this is bad because we have separate calculating of what TEvent is, rather that a single inference at the root? 🤔

| ListenerObject<InferEvent<TTarget, TType>>
| { (this: TTarget, ev: InferEvent<TTarget, TType>): void };

export type Binding<TTarget extends EventTarget = EventTarget, TType extends string = string> = {
type: TType;
listener: Listener<TTarget, InferEvent<TTarget, TType>>;
listener: Listener<TTarget, TType>;
options?: boolean | AddEventListenerOptions;
};
9 changes: 5 additions & 4 deletions test/type-tests/bind-all.usage-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expectTypeOf } from 'expect-type';
import { bindAll } from '../../src';

// inline definitions
Expand All @@ -23,14 +24,14 @@ import { bindAll } from '../../src';
bindAll(button, [
{
type: 'click',
listener(event: MouseEvent) {
const value: number = event.button;
listener(event) {
expectTypeOf(event).toEqualTypeOf<MouseEvent>();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Andarist this is currently failing as event is being inferred by TS as any

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestion on how to resolve this?

},
},
{
type: 'keydown',
listener(event: KeyboardEvent) {
const value: string = event.key;
listener(event) {
expectTypeOf(event).toEqualTypeOf<KeyboardEvent>();
},
},
]);
Expand Down
27 changes: 26 additions & 1 deletion test/type-tests/bind.usage-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bind } from '../../src';
import { expectTypeOf } from 'expect-type';

// inline definitions
{
Expand All @@ -10,14 +11,38 @@ import { bind } from '../../src';
});
}

// inline definitions (inferred)
{
const button: HTMLElement = document.createElement('button');

bind(button, {
type: 'keydown',
listener(event) {
expectTypeOf(event).toEqualTypeOf<KeyboardEvent>();
},
});
}

// inline definitions (inferred + no concrete event type)
{
const button: HTMLElement = document.createElement('button');

bind(button, {
type: 'hello',
listener(event) {
expectTypeOf(event).toEqualTypeOf<Event>();
},
});
}

// inferred types
{
const button: HTMLElement = document.createElement('button');

bind(button, {
type: 'click',
listener(event) {
const value: number = event.button;
expectTypeOf(event).toEqualTypeOf<MouseEvent>();
},
});
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1261,10 +1261,10 @@ exit@^0.1.2:
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
integrity sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=

expect-type@^0.13.0:
version "0.13.0"
resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-0.13.0.tgz#916646a7a73f3ee77039a634ee9035efe1876eb2"
integrity sha512-CclevazQfrqo8EvbLPmP7osnb1SZXkw47XPPvUUpeMz4HuGzDltE7CaIt3RLyT9UQrwVK/LDn+KVcC0hcgjgDg==
expect-type@^0.15.0:
version "0.15.0"
resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-0.15.0.tgz#89f75e22c88554844ea2b2faf4ef5fc2e579d3b5"
integrity sha512-yWnriYB4e8G54M5/fAFj7rCIBiKs1HAACaY13kCz6Ku0dezjS9aMcfcdVK2X8Tv2tEV1BPz/wKfQ7WA4S/d8aA==

expect@^27.4.6:
version "27.4.6"
Expand Down