Skip to content

Commit 4746c3d

Browse files
committed
More specific data types - ArrayBuffer instead of ArrayBufferLike
By default, TypeScript interfaces like `DataView` and `Uint8Array` are used with the more generic `ArrayBufferLike` type, representing that they can work with either `ArrayBuffer` or `SharedArrayBuffer`. TypeScript 5.9 now uses stricter types for interfaces such as the DOM `fetch` method: a DOM `BufferSource` is now (correctly) marked as requiring `ArrayBuffer`, not `SharedArrayBuffer`. As a result of that change, trying to use a @msgpack/msgpack `encode` result with a `fetch` call will result in a type error. This change updates the types in the `Encoder` class to use `ArrayBuffer` instead of `ArrayBufferLike`, which reflects their implementation and prevents issues with the newer, stricter interfaces.
1 parent 0e02917 commit 4746c3d

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/Encoder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ export class Encoder<ContextType = undefined> {
8484
private readonly forceIntegerToFloat: boolean;
8585

8686
private pos: number;
87-
private view: DataView;
88-
private bytes: Uint8Array;
87+
private view: DataView<ArrayBuffer>;
88+
private bytes: Uint8Array<ArrayBuffer>;
8989

9090
private entered = false;
9191

@@ -132,7 +132,7 @@ export class Encoder<ContextType = undefined> {
132132
*
133133
* @returns Encodes the object and returns a shared reference the encoder's internal buffer.
134134
*/
135-
public encodeSharedRef(object: unknown): Uint8Array {
135+
public encodeSharedRef(object: unknown): Uint8Array<ArrayBuffer> {
136136
if (this.entered) {
137137
const instance = this.clone();
138138
return instance.encodeSharedRef(object);
@@ -152,7 +152,7 @@ export class Encoder<ContextType = undefined> {
152152
/**
153153
* @returns Encodes the object and returns a copy of the encoder's internal buffer.
154154
*/
155-
public encode(object: unknown): Uint8Array {
155+
public encode(object: unknown): Uint8Array<ArrayBuffer> {
156156
if (this.entered) {
157157
const instance = this.clone();
158158
return instance.encode(object);

src/encode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { SplitUndefined } from "./context.ts";
1111
export function encode<ContextType = undefined>(
1212
value: unknown,
1313
options?: EncoderOptions<SplitUndefined<ContextType>>,
14-
): Uint8Array {
14+
): Uint8Array<ArrayBuffer> {
1515
const encoder = new Encoder(options);
1616
return encoder.encodeSharedRef(value);
1717
}

0 commit comments

Comments
 (0)