Skip to content

use-after-consume ​

A value used again on a path where an operation used it up: an ArrayBuffer after transfer() or after it went in a transfer list (worker.postMessage(buf, [buf]), structuredClone(x, { transfer: [buf] })), an iterator iterated a second time (a generator object, list.values(), text.matchAll(re)), a Response or Request body read twice, a ReadableStream read after getReader() locked it, a WebSocket, MessagePort, Node FileHandle or readline interface used after close(). On by default, as an error.

Why: TypeScript's types don't change when a value is used up: buf is still an ArrayBuffer after buf.transfer(), res still a Response after res.text(). What happens next is a TypeError (a view on a detached buffer, a body read again, a locked stream), or worse, nothing at all: the second pass over an iterator finds it empty, a detached buffer's length is 0, a message sent on a closed socket is dropped without a word. All of it passes tsc strict.

❌ Rejected: the second loop sees nothing, so second is 0

✕ Rejecteduse-after-consume
ts
function* readings(): Generator<number> {
  yield 3;
  yield 4;
}

export function compare(): number {
  const all = readings();
  let first = 0;
  for (const n of all) first += n;
  let second = 0;
  for (const n of all) second += n;
  return first - second;
}

✅ Accepted: collect it once, or make a new iterator for each pass

✓ Accepted0 diagnostics
ts
function* readings(): Generator<number> {
  yield 3;
  yield 4;
}

export function compare(): number {
  const all = [...readings()];
  let first = 0;
  for (const n of all) first += n;
  let second = 0;
  for (const n of readings()) second += n;
  return first - second;
}

❌ Rejected: the buffer went to the worker, so the view throws TypeError

✕ Rejecteduse-after-consume
ts
import { isErr } from "@evobug_com/safe-ts-std/result";
import { postMessage } from "@evobug_com/safe-ts-feature-browser/workers";

export function send(worker: Worker, pixels: ArrayBuffer): number {
  const sent = postMessage(worker, pixels, [pixels]);
  if (isErr(sent)) return 0;
  return new Uint8Array(pixels).length;
}

✅ Accepted: read what you need first (or transfer a copy, pixels.slice(0))

✓ Accepted0 diagnostics
ts
import { isErr } from "@evobug_com/safe-ts-std/result";
import { postMessage } from "@evobug_com/safe-ts-feature-browser/workers";

export function send(worker: Worker, pixels: ArrayBuffer): number {
  const size = pixels.byteLength;
  const sent = postMessage(worker, pixels, [pixels]);
  return isErr(sent) ? 0 : size;
}

❌ Rejected: the buffer went through its view, so the view is empty now

✕ Rejecteduse-after-consume
ts
import { isErr } from "@evobug_com/safe-ts-std/result";
import { postMessage } from "@evobug_com/safe-ts-feature-browser/workers";

export function render(worker: Worker, width: number): number {
  const pixels = new Uint8Array(width * 4);
  const sent = postMessage(worker, { pixels }, [pixels.buffer]);
  return isErr(sent) ? 0 : pixels.length;
}

✅ Accepted: what you need afterwards, read before

✓ Accepted0 diagnostics
ts
import { isErr } from "@evobug_com/safe-ts-std/result";
import { postMessage } from "@evobug_com/safe-ts-feature-browser/workers";

export function render(worker: Worker, width: number): number {
  const pixels = new Uint8Array(width * 4);
  const size = pixels.length;
  const sent = postMessage(worker, { pixels }, [pixels.buffer]);
  return isErr(sent) ? 0 : size;
}

❌ Rejected: a body can be read once; the second read is an Err every time

✕ Rejecteduse-after-consume
ts
import { isErr } from "@evobug_com/safe-ts-std/result";
import { json, text } from "@evobug_com/safe-ts-feature-browser/fetch";

export async function show(res: Response): Promise<string> {
  const raw = await text(res);
  const data = await json(res);
  return isErr(raw) || isErr(data) ? "" : raw;
}

✅ Accepted: read it once and keep what it gave, or clone() it before the first read

✓ Accepted0 diagnostics
ts
import { isErr } from "@evobug_com/safe-ts-std/result";
import { text } from "@evobug_com/safe-ts-feature-browser/fetch";

export async function show(res: Response): Promise<string> {
  const raw = await text(res);
  return isErr(raw) ? "" : `${res.status}: ${raw}`;
}

What uses a value up ​

The list is data, not code: the consumes sections of the language's map (crates/safe_ts_core/ecmascript/ecmascript-api.json) and of the Node and browser packs' maps, each entry run on Node (and in Chromium, Firefox and WebKit for the browser's own) by the tests. A platform's entries count where its pack is on.

ValueUsed up byThen
ArrayBuffertransfer(), transferToFixedLength()every use but buffer.detached: length 0, a view, slice(), resize(), transfer() throw TypeError
anything in a transfer liststructuredClone(x, { transfer }), worker.postMessage(m, transfer), port.postMessage, the global postMessage (a window's and a worker's), Node's worker_threadsevery use
a generator objectany iteration: for...of (also left early), spread, [a] = it, yield*, Array.from, new Set / new Map, Object.fromEntries, Promise.all, IteratorObject#toArray / forEach / reduce / some / every / findevery use: it gives nothing more
another one-shot iterator: values() / keys() / entries() of an array, Map, Set or typed array, matchAll, Iterator.from, an iterator helper's result, a value declared IterableIteratoran iteration that runs it to the end (a for...of that can't leave early, spread, Array.from, ...)every use
Response, Requesttext(), json(), arrayBuffer(), blob(), formData(), bytes() (and the packs' safe versions of them)another read rejects with TypeError, clone() throws one; status, headers and handing it on are fine
ReadableStreamgetReader(), tee(), pipeTo(), pipeThrough(), values() (until releaseLock())getReader(), tee(), pipeThrough() throw TypeError, pipeTo() and cancel() reject
ReadableStreamfor await...ofanother read gives nothing
WritableStreamgetWriter() (until releaseLock())getWriter() throws, close() and abort() reject
WebSocket, MessagePort, Workerclose(), terminate()send() / postMessage() send nothing, without an error
Node FileHandle (await open(...))close()every read and write rejects with Error [EBADF]
Node readline interfaceclose()question(), write(), prompt() throw (or reject) Error [ERR_USE_AFTER_CLOSE]

A Blob isn't used up (blob.text() reads it again), and neither are the segments Intl.Segmenter#segment gives.

A view made on a buffer (new Uint8Array(buf), new DataView(pixels.buffer)) before the buffer is transferred reads the detached buffer: a use of the view after the transfer is reported too (its length is 0, its elements undefined). A buffer is usually transferred through a view: worker.postMessage({ pixels }, [pixels.buffer]), structuredClone(values, { transfer: [values.buffer] }) and pixels.buffer.transfer() use up the view's buffer, so pixels and every other view the function made on that buffer are reported where they are used afterwards (pixels.buffer.detached is how to ask). A view made afterwards throws where it is made, which is the use reported.

A second name for the same value is the same value: after const same = buf; same.transfer(), buf is detached too. That holds while neither name is assigned again; a let that is, a property (this.buf) and a destructuring aren't followed.

An optional call (maybe?.transfer()) uses the value up where it ran: where maybe is there.

Paths ​

Uses are followed along the paths the code can take (how the checker follows paths): a use reached only where the operation didn't run isn't reported (if (flag) { const moved = buf.transfer(); return moved; } then a use of buf), a use on the next round of a loop whose round before used the value up is, and so is one after a try whose block may have used it up, or after a finally that does. Where the operation ran on some paths only, the message says "on one path to here". A new value assigned to the binding starts again; releaseLock() (on any reader or writer) ends a lock. One finding per binding: the first use.

What the rule can't see ​

  • What it can't tell the value is. A value is known from its declared type (res: Response, it: IterableIterator<T>, also a type imported from a Node module), from new X(...), from what a callable of the maps makes (items.values() with items declared as an array, await fetch(...), buf.transfer()), from a call of a function* of the same file. A generator function imported from another file, a value read from a field or an array element is unknown, and not reported. A method is never matched by its name alone: a class of yours with a transfer() is yours.
  • Another function. Only operations and uses written in the function itself count: a value handed to a function that uses it up isn't followed into it, and a callback (a nested function) runs some other time, so what it does with the value is neither "before" nor "after". A binding a nested function assigns isn't followed at all.
  • Handing it on after. Where every use fails (a detached buffer, a finished iterator), passing the value to a function is a use and is reported; where only some members fail (a response whose body was read), it isn't: the function may only read status.
  • Members, not paths through them. res.body after res.text() (a used stream) and a partial read with .next() aren't followed; neither is an iterator drained by IteratorObject#map and then read through the helper.
  • "Released on every exit" (a file closed on every path out) is the opposite question and a separate one; this rule only reports a use after the release.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra