Skip to content

Commit

Permalink
feat: Fix types for Vue 2.7
Browse files Browse the repository at this point in the history
The Vue 2.7 type for 'Vue' does not seem to fully match the Vue 2.6
'Vue' type. To cope with this, we also accept the new Vue 2.7
 'ComponentPublicInstance' type.

 Because the Vue 2.7 types refer to ES2015 types like 'Set', the
 tsconfig 'lib' needs to be ES2015 as well. This however should not
 change the compatability of the generated code of this library,
 as we only use ES5-style constructs.
  • Loading branch information
kkuegler committed Jul 28, 2022
1 parent 681a955 commit 311138a
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 14 deletions.
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type Vue from 'vue';
import type { ComponentPublicInstance } from 'vue';

// make EventCallback<string|number> = (x:string|number) => void instead of = ((x: string) => void) | ((x: number) => void)
// see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
type EventCallback<T = unknown> = [T] extends [undefined] ? () => void : (x: T) => void;

type GenericSource<T> = ((this: Vue, eventListener: EventCallback<T>) => void) & {
type GenericSource<T> = ((this: Vue | ComponentPublicInstance, eventListener: EventCallback<T>) => void) & {
addListener: (eventListener: EventCallback<T>) => void;
removeListener: (eventListener: EventCallback<T>) => void;
emit(data: T): void;
Expand All @@ -30,7 +30,7 @@ export function newEventSource<T>(): EventSource<T> {
}
}

const result = function (this: Vue /*the child Vue instance*/, cb: EventCallback<T>) {
const result = function (this: Vue | ComponentPublicInstance /*the child Vue instance*/, cb: EventCallback<T>) {
addListener(cb);
this.$once('hook:beforeDestroy', () => {
removeListener(cb);
Expand Down
173 changes: 164 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"devDependencies": {
"prettier": "2.5.0",
"typescript": "^4.5.2",
"vue": "^2.6.14"
"vue": "^2.7.8"
},
"files": [
"lib/**/*"
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"allowJs": true,
"moduleResolution": "node",
"baseUrl": ".",
"lib": ["es5", "DOM"],
"lib": ["ES2015", "DOM"],
"esModuleInterop": true,
"declaration": true
},
Expand Down

0 comments on commit 311138a

Please sign in to comment.