many-level icon indicating copy to clipboard operation
many-level copied to clipboard

In TypeScript, `Level` is not assignable to `AbstractLevel<Buffer, any, any>` when constructing `ManyLevelHost`

Open 6XGate opened this issue 1 year ago • 5 comments

Argument of type 'Level<string, string>' is not assignable to parameter of type 'AbstractLevel<Buffer, any, any>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Types of property 'batch' are incompatible.
    Type '{ (operations: (AbstractBatchPutOperation<Level<string, string>, string, string> | AbstractBatchDelOperation<Level<string, string>, string>)[]): Promise<...>; (operations: (AbstractBatchPutOperation<...> | AbstractBatchDelOperation<...>)[], callback: NodeCallback<...>): void; <K = string, V = string>(operations: (Ab...' is not assignable to type '{ (operations: AbstractBatchOperation<AbstractLevel<Buffer, any, any>, any, any>[]): Promise<void>; (operations: AbstractBatchOperation<AbstractLevel<Buffer, any, any>, any, any>[], callback: NodeCallback<...>): void; <K = any, V = any>(operations: AbstractBatchOperation<...>[], options: AbstractBatchOptions<...>): ...'.
      Types of parameters 'operations' and 'operations' are incompatible.
        Type 'AbstractBatchOperation<AbstractLevel<Buffer, any, any>, any, any>[]' is not assignable to type '(AbstractBatchPutOperation<Level<string, string>, string, string> | AbstractBatchDelOperation<Level<string, string>, string>)[]'.
          Type 'AbstractBatchOperation<AbstractLevel<Buffer, any, any>, any, any>' is not assignable to type 'AbstractBatchPutOperation<Level<string, string>, string, string> | AbstractBatchDelOperation<Level<string, string>, string>'.
            Type 'AbstractBatchPutOperation<AbstractLevel<Buffer, any, any>, any, any>' is not assignable to type 'AbstractBatchPutOperation<Level<string, string>, string, string> | AbstractBatchDelOperation<Level<string, string>, string>'.
              Type 'AbstractBatchPutOperation<AbstractLevel<Buffer, any, any>, any, any>' is not assignable to type 'AbstractBatchPutOperation<Level<string, string>, string, string>'.
                Types of property 'sublevel' are incompatible.
                  Type 'AbstractSublevel<AbstractLevel<Buffer, any, any>, any, any, any> | undefined' is not assignable to type 'AbstractSublevel<Level<string, string>, any, any, any> | undefined'.
                    Type 'AbstractSublevel<AbstractLevel<Buffer, any, any>, any, any, any>' is not assignable to type 'AbstractSublevel<Level<string, string>, any, any, any>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
                      Types of property 'db' are incompatible.
                        Type 'AbstractLevel<Buffer, any, any>' is not assignable to type 'Level<string, string>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.ts(2379)

6XGate avatar Aug 04 '24 01:08 6XGate

What code results in this error?

vweevers avatar Aug 04 '24 07:08 vweevers

Just the simple instantiation.

    const db = new Level(name)
    const host = new ManyLevelHost(db)

The tsconfig.json used was from the @tsconfig/strictest package.

6XGate avatar Aug 04 '24 14:08 6XGate

We can change the AbstractLevel<Buffer, any, any> to AbstractLevel<any, any, any> here:

https://github.com/Level/many-level/blob/a56d72df52c3f9d301a0885705c5dc223576ef31/index.d.ts#L74

My intent was probably to enforce that the given database supports Buffer, but that's not really a job for TypeScript and it does not actually affect the ManyLevelHost API (which is just a constructor and one method).

Could you try this out and send a PR if it works?

vweevers avatar Aug 04 '24 14:08 vweevers

That does seem to work, but if it's not desired to be typed that way, the documentation could mention to do new Level<Buffer> or new Level<Buffer, /* key, value types */> as a blurb f for TypeScript usage.

6XGate avatar Aug 06 '24 02:08 6XGate

That does seem to work

Great!

if it's not desired to be typed that way, the documentation could mention to do new Level<Buffer> or new Level<Buffer, /* key, value types */> as a blurb f for TypeScript usage.

Nah, the current type does not benefit users. Let's change it to any.

vweevers avatar Aug 06 '24 06:08 vweevers