Files
scriptc/tests/diagnostics/typed-array-undeclared-members.ts
T
Chris Tateandsaboursepideh e3d31c2a65 Answer typed-array member probes and destructure into typed-array elements
A typed array or Buffer in a compiled program is always a built-in kind
(programs cannot subclass them, add own properties or change their
prototypes), so Node 24's prototype chains for them are known exactly.

- A read of a member the type does not declare (in JavaScript, or under
  @ts-ignore) answers undefined when that chain lacks the name, as Node's
  GetV does: the base64 and hex feature probes (`typeof bytes.toHex`) that
  libraries such as @noble/hashes make. Names Node has, including the
  Buffer prototype's members on a Uint8Array that may hold a Buffer, keep
  the fence.
- `"name" in bytes` with a literal member name answers from the same
  chains, and `bytes.BYTES_PER_ELEMENT` is the element size.
- Array destructuring assignment into typed-array elements
  (`[high[i], low[i]] = [h, l]`) evaluates the receiver, the index and the
  value in order and writes the element as `b[i] = v` does.

Refs #1437

Co-authored-by: saboursepideh <25549001+saboursepideh@users.noreply.github.com>
2026-10-21 02:28:56 -05:00

9 lines
417 B
TypeScript

// Members a typed array's type does not declare read as undefined only when
// Node's prototype chain lacks them for every value of the type. A Uint8Array
// may hold a Buffer, whose prototype has asciiSlice, so that read and that
// `in` test depend on the value and stay fenced.
const bytes: Uint8Array = Buffer.from("abc");
// @ts-ignore
console.log(typeof bytes.asciiSlice);
console.log("latin1Write" in bytes);