surrealdb / surrealdb.wasm

A WebAssembly engine for the SurrealDB JavaScript SDK
https://surrealdb.com
Apache License 2.0
112 stars 17 forks source link

Bug: surrealdbWasmEngines is not assignable to Surreal engines #88

Closed orimay closed 2 months ago

orimay commented 2 months ago

Describe the bug

This:

import { Emitter, Engine, Surreal, type EngineEvents } from 'surrealdb.js';
import { surrealdbWasmEngines } from 'surrealdb.wasm';

const db = new Surreal({
  engines: surrealdbWasmEngines({
    strict: false,
    capabilities: {
      guest_access: true,
      functions: true,
      network_targets: true,
    },
  }),
});

gives a type error:

Type '{ mem: new (emitter: Emitter<EngineEvents>) => { ready: Promise<void> | undefined; reader?: Promise<void> | undefined; status: ConnectionStatus; ... 8 more ...; readonly connected: boolean; }; indxdb: new (emitter: Emitter<...>) => { ...; }; }' is not assignable to type 'Engines'.
  Property 'mem' is incompatible with index signature.
    Type 'new (emitter: Emitter<EngineEvents>) => { ready: Promise<void> | undefined; reader?: Promise<void> | undefined; status: ConnectionStatus; ... 8 more ...; readonly connected: boolean; }' is not assignable to type 'new (emitter: Emitter<EngineEvents>) => Engine'.
      Construct signature return types '{ ready: Promise<void> | undefined; reader?: Promise<void> | undefined; status: ConnectionStatus; connection: { url?: URL | undefined; namespace?: string | undefined; database?: string | undefined; token?: string | undefined; }; ... 7 more ...; readonly connected: boolean; }' and 'Engine' are incompatible.
        The types of 'rpc' are incompatible between these types.
          Type '<Method extends string, Params extends unknown[], Result extends unknown>(request: RpcRequest<Method, Params>) => Promise<RpcResponse<Result>>' is not assignable to type '<Method extends string, Params extends unknown[] | undefined, Result extends unknown>(request: RpcRequest<Method, Params>) => Promise<RpcResponse<Result>>'.
            Types of parameters 'request' and 'request' are incompatible.
              Type 'RpcRequest<Method, Params>' is not assignable to type 'RpcRequest<Method, unknown[]>'.
                Type 'Params' is not assignable to type 'unknown[]'.
                  Type 'unknown[] | undefined' is not assignable to type 'unknown[]'.
                    Type 'undefined' is not assignable to type 'unknown[]'.ts(2322)

because typeEngine has a method

abstract rpc<Method extends string, Params extends unknown[] | undefined, Result extends unknown>(request: RpcRequest<Method, Params>): Promise<RpcResponse<Result>>;

which is

rpc<Method extends string, Params extends unknown[], Result extends unknown>(request: RpcRequest<Method, Params>): Promise<RpcResponse<Result>>;

for both mem and indxdb

Steps to reproduce

import { Emitter, Engine, Surreal, type EngineEvents } from 'surrealdb.js';
import { surrealdbWasmEngines } from 'surrealdb.wasm';

const db = new Surreal({
  engines: surrealdbWasmEngines({
    strict: false,
    capabilities: {
      guest_access: true,
      functions: true,
      network_targets: true,
    },
  }),
});

Expected behaviour

Should not error out. Currently, I use the following fix:

import { Emitter, Engine, Surreal, type EngineEvents } from 'surrealdb.js';
import { surrealdbWasmEngines } from 'surrealdb.wasm';

const db = new Surreal({
  engines: surrealdbWasmEngines({
    strict: false,
    capabilities: {
      guest_access: true,
      functions: true,
      network_targets: true,
    },
  }) as Record<string, new (emitter: Emitter<EngineEvents>) => Engine>,
});

SurrealDB version

surrealdb.js: 1.0.0-beta.9, surrealdb.wasm: 1.0.0-beta.12

Contact Details

dmitrii.a.baranov@gmail.com

Is there an existing issue for this?

Code of Conduct