Skip to content

Typescript 5.9.x introduced changes to the ArrayBuffer type #120

@quentinadam

Description

@quentinadam

See: https://devblogs.microsoft.com/typescript/announcing-typescript-5-9-rc/#notable-behavioral-changes

Notably, many standard APIs that expect a BufferSource will fail at a type-level when supplied with a default Uint8Array instead of Uint8Array<ArrayBuffer>.

For example the following code does not compile anymore in Typescript 5.9.x:

function createBuffer1(): Uint8Array {
    return new Uint8Array(1);
}

fetch('url', { body: createBuffer1() });

But the following one does:

function createBuffer2(): Uint8Array<ArrayBuffer> {
    return new Uint8Array(1);
}

fetch('url', { body: createBuffer2() });

See: https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABBATgUwIZTQIRMYNFARgAoBKALkQFUYwoAOAQRRQwE9EBvAKEQGJ0UECiRg0Ad1r0mrdh1LFyAbl4BfXr0JQIAC1IByUQBtDAGh6IARnAAmHaqkzY8BImXKJ1qraEiwCMjoWLj4hCgATBTUdAwsbJwAPPKcbhEAfDz8gsKi4lIy8amKymqa2mi6BsYoZpbcNvaOwS5h7lEU3r5AA

In particular the @noble/hashes library specifies the return types of the functions that return buffers as type Uint8Array, which then requires the library consumers to explicitly type-cast to Uint8Array<ArrayBuffer>, which is not best practice.

Would there be any chance to update the return types to Uint8Array<ArrayBuffer> ?

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