Skip to content

promise-as-value ​

A promise used where the value it resolves to is meant: an async call's result put in a template, tested with !, compared with undefined, handed to isErr or q, or an async function handed where its answer is read right away (filter, some, find, reduce, forEach, new Promise, a function of yours that tests its callback's answer). On by default, as an error.

Why: a promise is an object: always truthy, never undefined, [object Promise] as text. TypeScript lets it through wherever a value of any type is accepted, and that includes std's Result: Result<T, E> is T | Err<E>, so a Promise<Result<T, E>> is a Result too, whose value is the promise. isErr(load()) is always false, and the error it resolves to is lost. A built-in that reads its callback's answer before the callback's promise settles does the same inside: ids.filter(async (id) => ...) keeps every id. Guide: Async.

❌ Rejected: the badge shows [object Promise] new, and every id is kept

✕ Rejectedpromise-as-value
ts
async function unread(inbox: string): Promise<number> {
  return inbox.length;
}

async function allowed(id: string): Promise<boolean> {
  return id.startsWith("u-");
}

export function badge(inbox: string): string {
  return `${unread(inbox)} new`;
}

export function visible(ids: readonly string[]): string[] {
  return ids.filter(async (id) => await allowed(id));
}

✅ Accepted: await the value, and decide each item where its answer is known

✓ Accepted0 diagnostics
ts
async function unread(inbox: string): Promise<number> {
  return inbox.length;
}

async function allowed(id: string): Promise<boolean> {
  return id.startsWith("u-");
}

export async function badge(inbox: string): Promise<string> {
  return `${await unread(inbox)} new`;
}

export async function visible(ids: readonly string[]): Promise<string[]> {
  const kept: string[] = [];
  for (const id of ids) if (await allowed(id)) kept.push(id);
  return kept;
}

std's functions that read a value refuse a promise in their types, so tsc stops isErr(load()), q(load()), unwrapOr(load(), 0), mapErr, isSome, isPanic, describeError, expectPresent and the generated validators' isJsonErr itself, with the message this is a promise: await it, then pass the value (match, is and variant need a tag, which a promise doesn't have). The rule reports the same calls, for the editor and the fix that writes the await. A Result whose value is a promise on purpose, Result<Promise<T>, E> with its E written out, is let through.

The refusal is a type parameter each of these functions has last, which nothing passes; std declares it with a @ts-expect-error that every project's tsc compiles. npm run test:std compiles std and a project using it with TypeScript 7 (the repository's) and 5.9; 5.5 (with skipLibCheck, for its @types/node), 5.6, 5.7, 5.8 and 6.0 were checked by hand. Generic code is let through, as it must be: TypeScript can't tell whether a type parameter is a promise. Explicit type arguments that are type parameters can't be: isErr<T, E>(result) or unwrapOr<T, E>(result, fallback) inside a generic function fails with Argument of type 'Result<T, E>' is not assignable to parameter of type 'NotAPromise<CheckResult<T, E>, Result<T, E>>', which doesn't mention a promise. Drop the type arguments: isErr(result).

A function of yours that reads what its callback returns is the same, when its parameter's type lets an async function through: a return type of unknown, {}, object, a type parameter, or Result<T, E> of one. A return type of boolean (or number, string) makes tsc refuse the async function itself.

❌ Rejected: a promise passes the test, so every item is counted

✕ Rejectedpromise-as-value
ts
async function overdue(invoice: string): Promise<boolean> {
  return invoice.endsWith("!");
}

function countWhere<T>(items: readonly T[], accept: (item: T) => unknown): number {
  let n = 0;
  for (const item of items) if (accept(item)) n++;
  return n;
}

export function late(invoices: readonly string[]): number {
  return countWhere(invoices, overdue);
}

✅ Accepted: the parameter says it is a promise, and the function awaits it

✓ Accepted0 diagnostics
ts
async function overdue(invoice: string): Promise<boolean> {
  return invoice.endsWith("!");
}

async function countWhere<T>(items: readonly T[], accept: (item: T) => Promise<boolean>): Promise<number> {
  let n = 0;
  for (const item of items) if (await accept(item)) n++;
  return n;
}

export function late(invoices: readonly string[]): Promise<number> {
  return countWhere(invoices, overdue);
}

What is reported, next to what TypeScript says on its own:

The promise isTypeScriptpromise-as-value
if (p), p ? a : b, p && xrefuses it (TS2801)leaves it to TypeScript
p + 1, p * 2, p < 2refuses it (TS2365, TS2362)leaves it
p === 1, p === n (n: number)refuses it (TS2367)leaves it
switch (p) { case 1: }refuses it (TS2678)leaves it
p.length, p.name, const { id } = p, for (x of p)refuses it (TS2339, TS2488)leaves it
isErr(p), q(p), unwrapOr(p, x), mapErr(p, f), isSome(p), isPanic(p)refuses it (std's types)reports it, with Add await
match(p, ...), is(p, ...), variant(p, ...)refuses it (no tag)reports it, with Add await
`${p}`, "text" + paccepts it: [object Promise]reports it
!paccepts it: always falsereports it
p || x, p ?? xaccepts it: always preports it
p === undefined, p != nullaccepts it: always the same answerreports it
String(p), Number(p), Boolean(p), JSON.stringify(p)accepts itreports it
{ ...p }accepts it: copies nothingreports it
while (p), for (; p; )accepts it: never stopsreports it
filter, some, every, find, findIndex, findLast, findLastIndex (also of an iterator) given an async callbackaccepts it: a promise passes every testreports it
reduce(async (sum, x) => ...) that doesn't await sumaccepts itreports it
forEach(async ...), new Promise(async ...)accepts it: the promises are droppedreports it (unless the failure inside is already reported)
items.map(async ...) read as values (.filter(Boolean), .some(...), .join(), .includes(x), a template), also through a const holding itaccepts itreports it; Promise.all(items.map(...)), .length, .slice(), an index are fine
expect(p).toBeTruthy() (any matcher but toBe, toBeInstanceOf, without .resolves / .rejects), assert(p), assert.ok(p), assert.equal(p, x) of node:assertaccepts it: the test passes whatever p resolves toreports it, with Add await
Map.groupBy, JSON.parse's reviver, JSON.stringify's replacer, given an async callbackaccepts itreports it
sort, toSorted, a typed array's map or from, replace, Object.groupBy, given an async callbackrefuses it (lib types the answer)leaves it

p is a call the checker knows returns a promise: a function of the project that is async or declared to return a Promise (also through const loader = load, and a function that returns what such a function returns), a const holding one (const pending = load(); if (!pending) ...), Promise.resolve(...) and the other statics, a .then(...) chain, or a package's or the platform's function its pack says rejects (fetch); also a call through a declared function type (flush: () => Promise<void> of an interface, this.sink?.flush()). Which built-ins read their callback's answer, and how, is data: the callbacks part of the built-ins map, each one shown on the running Node by a test.

A method is the built-in's when its receiver is one by the type the code writes: an array, a tuple, a typed array, a Map or Set, an iterator or generator, a DOM list (NodeList, Headers, ...), also through a type alias, a declared return type (someIds().filter(...)) or a chain of the collection's methods (list.map(...).filter(...)). A receiver with another written type is not: a class or interface of the project with a filter of its own, a package's Readable (whose filter and map await what they are given), what a package's function returns. A receiver whose type isn't written (an unannotated local made by something the checker doesn't follow) is taken for the built-in, because it almost always is; on an adopter's code (about 760 files) no call with an async or promise-returning callback was reported this way.

Quick fix: Add await (in an async function), where the promise is read.

What the rule can't see: a promise stored in a let, a field or an array and read later, or returned by a function whose return type isn't written and that isn't followed; a callback handed to a package's function; a method named like a built-in one (filter) on a value whose type isn't written, which it takes to be the built-in (above). p === other with another value of unknown type, which is always false for an async call, isn't reported. A dropped promise is floating-promise (or floating-rejection, when it can reject).

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