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

TypeScript parameter type inconsistency for IDL.decode #975

Open
Tracked by #2693
bdemann opened this issue Feb 10, 2025 · 0 comments
Open
Tracked by #2693

TypeScript parameter type inconsistency for IDL.decode #975

bdemann opened this issue Feb 10, 2025 · 0 comments

Comments

@bdemann
Copy link

bdemann commented Feb 10, 2025

Request
The parameter types for IDL.decode should be updated to reflect the full range of types it can process at runtime. Ideally, it should accept all types it currently supports (Uint8Array<ArrayBufferLike> and ArrayBufferLike). At the very least, it should be made consistent with the actual runtime return type of IDL.encode (Uint8Array<ArrayBufferLike>).

Describe the bug
The parameter types of IDL.decode seem overly strict and inconsistent with IDL.encode. As discussed in dfinity/agent-js#950, the runtime return type of IDL.encode is actually Uint8Array not ArrayBuffer.

At runtime, this isn't an issue because IDL.decode can handle Uint8Array<ArrayBuffer>, Uint8Array<SharedArrayBuffer>, ArrayBuffer, and SharedArrayBuffer. Previously, type checking wasn't a problem because IDL.encode was typed as returning ArrayBuffer, which matched IDL.decode's expected input. This meant they aligned as ArrayBuffer at type check time and as Uint8Array<ArrayBufferLike> at runtime.

However, with the latest TypeScript transpilation target (ES2024), ArrayBuffer and SharedArrayBuffer (the two types that make up the ArrayBufferLike union) have diverged. This makes the discrepancies more pronounced, even though IDL.decode can work with any ArrayBuffer type or Uint8Array at runtime.

To Reproduce
Steps to reproduce the behavior:

  • Version: 2.2.0
const myUint8Array: Uint8Array<ArrayBuffer> = new Uint8Array([
    68, 73, 68, 76, 0, 0
]);
const myArrayBuffer = myUint8Array.buffer;
const mySharedArrayBuffer = new SharedArrayBuffer(6);
const mySharedUint8Array: Uint8Array<SharedArrayBuffer> = new Uint8Array(mySharedArrayBuffer);
mySharedUint8Array.set(myUint8Array);

// @ts-ignore
const decodedUint8Array = IDL.decode([], myUint8Array);
console.log('decodedUint8Array', decodedUint8Array);
// @ts-ignore
const decodedArrayBuffer = IDL.decode([], myArrayBuffer);
console.log('decodedArrayBuffer', decodedArrayBuffer);
// @ts-ignore
const decodedSharedUint8Array = IDL.decode([], mySharedUint8Array);
console.log('decodedSharedUint8Array', decodedSharedUint8Array);
// @ts-ignore
const decodedSharedArrayBuffer = IDL.decode([], mySharedArrayBuffer);
console.log('decodedSharedArrayBuffer', decodedSharedArrayBuffer);
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