Skip to content

Type annotations on encode() returns Buffer in browser instead of Uint8Array #120

@ekzhang

Description

@ekzhang

Hi there! Thanks for this wonderful library. Just wanted to report what appears to be an inaccuracy in the type annotation for encode():

cbor-x/index.d.ts

Lines 61 to 63 in f531383

export function encode(value: any): Buffer
export function encodeAsIterable(value: any): Iterable<Buffer | Blob | AsyncIterable<Buffer>>
export function encodeAsAsyncIterable(value: any): AsyncIterable<Buffer>

It returns a Buffer, but when I call encode() in a browser, I get a Uint8Array instead:

image

This means that when you use it in TypeScript, I get an error when trying to type it as Uint8Array:

image

Type casting as workaround seems to resolve compile time issue:

// Note: The types for cbor-x are incorrect here, as encode() actually
// returns a Uint8Array in the browser. Cast to fix the type error.
const data = <Uint8Array>(encode(message) as unknown);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions