tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
85.17k stars 2.57k forks source link

[bug] mockIPC callback typing #11720

Open Daniel-Knights opened 5 hours ago

Daniel-Knights commented 5 hours ago

Describe the bug

The callback function passed to mockIPC seems incorrectly typed. Whichever way I pass the callback I get TypeScript errors.

Some questions I have:

This typing might be more appropriate if anything can be returned:

-export declare function mockIPC(cb: <T>(cmd: string, payload?: InvokeArgs) => Promise<T>): void;
+export declare function mockIPC(cb: (callId: string, args?: InvokeArgs) => unknown): void;

Reproduction

  1. Create a Tauri app that uses TypeScript
  2. Try either of the examples here: https://v2.tauri.app/reference/javascript/api/namespacemocks/#mockipc

Expected behavior

No TS errors.

Full tauri info output

[✔] Environment
    - OS: Mac OS 12.7.4 x86_64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.84.0-nightly (917a50a03 2024-11-15)
    ✔ cargo: 1.84.0-nightly (4a2d8dc63 2024-11-09)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: nightly-x86_64-apple-darwin (default)
    - node: 20.13.1
    - pnpm: 9.5.0
    - yarn: 1.22.10
    - npm: 10.5.2

[-] Packages
    - tauri 🦀: 2.1.1
    - tauri-build 🦀: 2.0.3
    - wry 🦀: 0.47.0
    - tao 🦀: 0.30.8
    - @tauri-apps/api : 2.1.1
    - @tauri-apps/cli : 2.1.0

[-] Plugins
    - tauri-plugin-shell 🦀: 2.0.2
    - @tauri-apps/plugin-shell : 2.0.1

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - bundler: Vite

Stack trace

Argument of type '<T>(cmd: string, payload: InvokeArgs | undefined) => Promise<any> | undefined' is not assignable to parameter of type '<T>(cmd: string, payload?: InvokeArgs | undefined) => Promise<T>'.
  Type 'Promise<any> | undefined' is not assignable to type 'Promise<T>'.
    Type 'undefined' is not assignable to type 'Promise<T>'.ts(2345)

After changing the return value to a promise:

Argument of type '() => Promise<void>' is not assignable to parameter of type '<T>(cmd: string, payload?: InvokeArgs | undefined) => Promise<T>'.
  Type 'Promise<void>' is not assignable to type 'Promise<T>'.
    Type 'void' is not assignable to type 'T'.
      'T' could be instantiated with an arbitrary type which could be unrelated to 'void'.ts(2345)

Additional context

No response

FabianLars commented 4 hours ago

I wrote a whole book here until i noticed that i am the reason for the weird types: https://github.com/tauri-apps/tauri/pull/8534 - i honestly can't remember that PR at all. That said i'd be surprised if i didn't test it back then but now i'm not able to write tests using this in typescript either 🙃

I'll try to look into it tomorrow.