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

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

Open
irbull opened this issue Oct 10, 2024 · 4 comments
Labels
needs investigation requires further investigation before determining if it is an issue or not types Issues related to TypeScript types

Comments

@irbull
Copy link
Contributor

irbull commented Oct 10, 2024

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 #22381

@irbull
Copy link
Contributor Author

irbull commented Oct 10, 2024

This checks out fine if I change the signature to:

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

@petamoriken
Copy link
Contributor

petamoriken commented Oct 11, 2024

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
Copy link
Contributor

@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
Copy link
Contributor Author

irbull commented Oct 14, 2024

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.

@lucacasonato lucacasonato added needs investigation requires further investigation before determining if it is an issue or not types Issues related to TypeScript types labels Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation requires further investigation before determining if it is an issue or not types Issues related to TypeScript types
Projects
None yet
Development

No branches or pull requests

3 participants