Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ekzhang opened this issue Dec 30, 2024 · 0 comments

Comments

@ekzhang
Copy link

ekzhang commented Dec 30, 2024

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant