From 4f5b9e7b825c0717764110b8edb87e94d2fc7889 Mon Sep 17 00:00:00 2001 From: Alex Reardon Date: Thu, 2 Feb 2023 14:44:37 +1100 Subject: [PATCH 1/3] running type tests as a part of validate command --- package.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 403e578..7fb5592 100644 --- a/package.json +++ b/package.json @@ -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", From 54001e5c8bb5c1defc56925cd9ee5a37197c3c7d Mon Sep 17 00:00:00 2001 From: Alex Reardon Date: Thu, 2 Feb 2023 15:04:23 +1100 Subject: [PATCH 2/3] WIP --- package.json | 2 +- test/type-tests/bind-all.usage-types.ts | 9 ++++--- test/type-tests/bind.usage-types.ts | 27 ++++++++++++++++++- .../public-types-function-listener-test.ts | 2 +- .../public-types-object-listener-test.ts | 2 +- yarn.lock | 8 +++--- 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 7fb5592..857a489 100644 --- a/package.json +++ b/package.json @@ -48,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", diff --git a/test/type-tests/bind-all.usage-types.ts b/test/type-tests/bind-all.usage-types.ts index 1c5950c..b8080fe 100644 --- a/test/type-tests/bind-all.usage-types.ts +++ b/test/type-tests/bind-all.usage-types.ts @@ -1,3 +1,4 @@ +import { expectTypeOf } from 'expect-type'; import { bindAll } from '../../src'; // inline definitions @@ -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(); }, }, { type: 'keydown', - listener(event: KeyboardEvent) { - const value: string = event.key; + listener(event) { + expectTypeOf(event).toEqualTypeOf(); }, }, ]); diff --git a/test/type-tests/bind.usage-types.ts b/test/type-tests/bind.usage-types.ts index cfed84e..f9de28d 100644 --- a/test/type-tests/bind.usage-types.ts +++ b/test/type-tests/bind.usage-types.ts @@ -1,4 +1,5 @@ import { bind } from '../../src'; +import { expectTypeOf } from 'expect-type'; // inline definitions { @@ -10,6 +11,30 @@ import { bind } from '../../src'; }); } +// inline definitions (inferred) +{ + const button: HTMLElement = document.createElement('button'); + + bind(button, { + type: 'keydown', + listener(event) { + expectTypeOf(event).toEqualTypeOf(); + }, + }); +} + +// inline definitions (inferred + no concrete event type) +{ + const button: HTMLElement = document.createElement('button'); + + bind(button, { + type: 'hello', + listener(event) { + expectTypeOf(event).toEqualTypeOf(); + }, + }); +} + // inferred types { const button: HTMLElement = document.createElement('button'); @@ -17,7 +42,7 @@ import { bind } from '../../src'; bind(button, { type: 'click', listener(event) { - const value: number = event.button; + expectTypeOf(event).toEqualTypeOf(); }, }); } diff --git a/test/type-tests/public-types-function-listener-test.ts b/test/type-tests/public-types-function-listener-test.ts index f6e7500..34145fa 100644 --- a/test/type-tests/public-types-function-listener-test.ts +++ b/test/type-tests/public-types-function-listener-test.ts @@ -50,7 +50,7 @@ declare global { options?: boolean | AddEventListenerOptions; }>(); - type ClickListener = Listener; + type ClickListener = Listener; // `this` and `event` are set correctly expectType void>>(true); diff --git a/test/type-tests/public-types-object-listener-test.ts b/test/type-tests/public-types-object-listener-test.ts index 9878aad..d5ed0a3 100644 --- a/test/type-tests/public-types-object-listener-test.ts +++ b/test/type-tests/public-types-object-listener-test.ts @@ -1,6 +1,6 @@ import { expectTypeOf } from 'expect-type'; import { Binding, Listener } from '../../src'; -import { expectType, TypeOf } from 'ts-expect'; +import { expectTypeOf } from 'expect-type'; // extracting event type { diff --git a/yarn.lock b/yarn.lock index b301860..79f061e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" From 0e950642e3d7d1ee2e0840954934e66b2962695f Mon Sep 17 00:00:00 2001 From: Alex Reardon Date: Thu, 2 Feb 2023 15:14:26 +1100 Subject: [PATCH 3/3] trying to move type signatures of helpers back for less change --- src/bind-all.ts | 9 +++------ src/bind.ts | 2 +- src/types.ts | 8 ++++---- test/type-tests/public-types-function-listener-test.ts | 2 +- test/type-tests/public-types-object-listener-test.ts | 2 +- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/bind-all.ts b/src/bind-all.ts index 0799362..57e1605 100644 --- a/src/bind-all.ts +++ b/src/bind-all.ts @@ -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 { @@ -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; }; diff --git a/src/bind.ts b/src/bind.ts index 1b8bc1e..67fb9c7 100644 --- a/src/bind.ts +++ b/src/bind.ts @@ -22,7 +22,7 @@ export function bind>; + listener: Listener; options?: boolean | AddEventListenerOptions; }, ): UnbindFn { diff --git a/src/types.ts b/src/types.ts index 6ead631..5a2ff3b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -31,12 +31,12 @@ type ListenerObject = { }; // event listeners can be an object or a function -export type Listener = - | ListenerObject - | { (this: TTarget, ev: TEvent): void }; +export type Listener = + | ListenerObject> + | { (this: TTarget, ev: InferEvent): void }; export type Binding = { type: TType; - listener: Listener>; + listener: Listener; options?: boolean | AddEventListenerOptions; }; diff --git a/test/type-tests/public-types-function-listener-test.ts b/test/type-tests/public-types-function-listener-test.ts index 34145fa..f6e7500 100644 --- a/test/type-tests/public-types-function-listener-test.ts +++ b/test/type-tests/public-types-function-listener-test.ts @@ -50,7 +50,7 @@ declare global { options?: boolean | AddEventListenerOptions; }>(); - type ClickListener = Listener; + type ClickListener = Listener; // `this` and `event` are set correctly expectType void>>(true); diff --git a/test/type-tests/public-types-object-listener-test.ts b/test/type-tests/public-types-object-listener-test.ts index d5ed0a3..9878aad 100644 --- a/test/type-tests/public-types-object-listener-test.ts +++ b/test/type-tests/public-types-object-listener-test.ts @@ -1,6 +1,6 @@ import { expectTypeOf } from 'expect-type'; import { Binding, Listener } from '../../src'; -import { expectTypeOf } from 'expect-type'; +import { expectType, TypeOf } from 'ts-expect'; // extracting event type {