neovim / node-client

Nvim Node.js client and plugin host
https://neovim.io/node-client/
MIT License
464 stars 49 forks source link

client.executeLua() might return a singleton (like "string") which is incompatible with "object" #346

Closed saidelike closed 2 months ago

saidelike commented 3 months ago

The prototype of executeLua is:

    /**
     * Alias for `lua()` to be consistent with neovim API
     */
    executeLua(code: string, args?: VimValue[]): Promise<object>;

However when calling executeLua, it is possible that it returns a string. So we have to do something like this casting:

const data = await client.executeLua(`return "hello"), []) as unknown as string;

which is not great.

Indeed, it works great with arrays as we can use that casting:

  const [val1, val2] = (await client.executeLua(
    luaCode,
    [],
  )) as [number, boolean];

but when only a single value is returned, we can't cast it without using the ugly "unknown" syntax.

justinmk commented 3 months ago

Should we change the return type to Promise<any> ? Or maybe a sum type.

saidelike commented 3 months ago

A sum type seems good?

justinmk commented 3 months ago

I have an upcoming PR https://github.com/neovim/node-client/pull/338 to fix all the type errors (this project currently doesn't enforce strict), this could follow that.

justinmk commented 2 months ago

see https://github.com/neovim/node-client/pull/353