diff --git a/.changes/improve-api-types.md b/.changes/improve-api-types.md new file mode 100644 index 00000000000..f9a4d1d56e5 --- /dev/null +++ b/.changes/improve-api-types.md @@ -0,0 +1,5 @@ +--- +"api": patch +--- + +Improve `EventName` type using `type-fest`'s `LiteralUnion`. diff --git a/tooling/api/package.json b/tooling/api/package.json index 9adcd1b69ee..088ba9bb7ce 100644 --- a/tooling/api/package.json +++ b/tooling/api/package.json @@ -66,6 +66,7 @@ "rollup": "2.56.1", "rollup-plugin-terser": "7.0.2", "tslib": "2.3.0", + "type-fest": "2.0.0", "typedoc": "0.21.5", "typedoc-plugin-markdown": "3.10.4", "typescript": "4.3.5" diff --git a/tooling/api/src/event.ts b/tooling/api/src/event.ts index 2909ddfb964..da614db2766 100644 --- a/tooling/api/src/event.ts +++ b/tooling/api/src/event.ts @@ -12,6 +12,7 @@ import { invokeTauriCommand } from './helpers/tauri' import { emit as emitEvent } from './helpers/event' import { transformCallback } from './tauri' +import { LiteralUnion } from 'type-fest' interface Event { /** Event name */ @@ -22,7 +23,7 @@ interface Event { payload: T } -type EventName = +type EventName = LiteralUnion< | 'tauri://update' | 'tauri://update-available' | 'tauri://update-install' @@ -37,8 +38,9 @@ type EventName = | 'tauri://menu' | 'tauri://file-drop' | 'tauri://file-drop-hover' - | 'tauri://file-drop-cancelled' - | string + | 'tauri://file-drop-cancelled', + string +> type EventCallback = (event: Event) => void diff --git a/tooling/api/src/window.ts b/tooling/api/src/window.ts index 02f3462bd8a..b51c38d1afa 100644 --- a/tooling/api/src/window.ts +++ b/tooling/api/src/window.ts @@ -249,7 +249,7 @@ class WebviewWindowHandle { ): Promise { if (this._handleTauriEvent(event, handler)) { return Promise.resolve(() => { - // eslint-disable-next-line security/detect-object-injection + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection const listeners = this.listeners[event] listeners.splice(listeners.indexOf(handler), 1) }) diff --git a/tooling/api/tsconfig.json b/tooling/api/tsconfig.json index 7488ac8bc5b..0a2ec4d5a1a 100644 --- a/tooling/api/tsconfig.json +++ b/tooling/api/tsconfig.json @@ -10,7 +10,8 @@ }, "declaration": true, "declarationDir": "dist", - "rootDir": "src" + "rootDir": "src", + "moduleResolution": "node" }, "include": ["./src"] } diff --git a/tooling/api/yarn.lock b/tooling/api/yarn.lock index 7bc7ea50d25..7b3ae6dcf0d 100644 --- a/tooling/api/yarn.lock +++ b/tooling/api/yarn.lock @@ -3377,6 +3377,11 @@ type-check@^0.4.0, type-check@~0.4.0: dependencies: prelude-ls "^1.2.1" +type-fest@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.0.0.tgz#e9daf5615e89f6d430f34117f88f4ee2cd5a2725" + integrity sha512-BoEUnckjP9oiudy3KxlGdudtBAdJQ74Wp7dYwVPkUzBn+cVHOsBXh2zD2jLyqgbuJ1KMNriczZCI7lTBA94dFg== + type-fest@^0.20.2: version "0.20.2" resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4"