denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
96.34k stars 5.32k forks source link

'ArrayBufferView' is not assignable to parameter of type 'string | ArrayBufferView #26129

Open irbull opened 1 week ago

irbull commented 1 week ago

Version: Deno 2.0

While trying to build an Astro site with Deno 2.0 I hit an error about mismatched types. I have managed to create a reproducible snippet that should help track this down:

import { writeFileSync } from "node:fs";

writeFileSync("buffer.txt", returnBuffer());

function returnBuffer(): ArrayBufferView {
    return new Uint8Array([1, 2, 3, 4, 5]);
}

This seems to be valid typescript, but deno check fails with:

error: TS2345 [ERROR]: Argument of type 'ArrayBufferView' is not assignable to parameter of type 'string | ArrayBufferView'.
  Type 'ArrayBufferView' is missing the following properties from type 'DataView': getFloat32, getFloat64, getInt8, getInt16, and 19 more.
writeFileSync("buffer.txt", returnBuffer());

This could be a duplicate of https://github.com/denoland/deno/issues/22381

irbull commented 1 week ago

This checks out fine if I change the signature to:

function returnBuffer(): NodeJS.ArrayBufferView {
    return new Uint8Array([1, 2, 3, 4, 5]);
}
petamoriken commented 1 week ago

Breaking changes may have occurred in @types/node for TypeScript 5.7 Beta support. https://devblogs.microsoft.com/typescript/announcing-typescript-5-7-beta/#typedarrays-are-now-generic-over-arraybufferlike

petamoriken commented 3 days ago

@irbull NodeJS.ArrayBufferView type seems to be stricter than the ArrayBufferView type. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a8bde6b92a06d9da2735a04ab611058bfa21c20b/types/node/globals.d.ts#L486-L498

So, this is a TypeScript issue rather than a Deno issue.

irbull commented 3 days ago

But it's unclear to me what type (ArrayBufferView or NodeJS.ArrayBufferView) should be used when using the node.js shims. This same code works fine in Node (it's actually part of my Astro Build website), but the code doesn't build properly with Deno. It seems writeFileSync wants a NodeJSArrayBufferView.