Skip to content

q-needs-result-return ​

q(...) is used in a function that doesn't declare a Result return type.

Why: q returns the error from the enclosing function, so that function must be able to return one: its return type must be Result<T, E> or Promise<Result<T, E>>. Note that q returns from the innermost function, which for code in a callback is the callback. Guide: Errors.

ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

function parsePort(text: string): Result<number, PortError> {
  const port = Number(text);
  return Number.isInteger(port) ? port : err(PortError.NotAPort({ text }));
}

export function portOrDefault(text: string): number {
  const port = q(parsePort(text));
  return port;
}

✅ Accepted: return a Result, or handle the error here

✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, q, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

function parsePort(text: string): Result<number, PortError> {
  const port = Number(text);
  return Number.isInteger(port) ? port : err(PortError.NotAPort({ text }));
}

export function port(text: string): Result<number, PortError> {
  const value = q(parsePort(text));
  return value;
}

export function portOrDefault(text: string): number {
  const value = parsePort(text);
  return isErr(value) ? 8080 : value;
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { err, isErr, q, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

function parsePort(text: string): Result<number, PortError> {
  const port = Number(text);
  return Number.isInteger(port) ? port : err(({ tag: "NotAPort", text } satisfies PortError as PortError));
}

export function port(text: string): Result<number, PortError> {
  const __q1 = parsePort(text); if (__q1 instanceof __qErr) return __q1; const value = __q1;
  return value;
}

export function portOrDefault(text: string): number {
  const value = parsePort(text);
  return isErr(value) ? 8080 : value;
}

The declared return type may also be an alias, in the same file or imported (type Reply = Promise<Result<string, GreetError>>; async function greet(id): Reply, or Promise<Lookup> with type Lookup = Result<number, LookupError>).

A callback can also get its return type from where it is written, without an annotation of its own:

  • the type annotation of the const it initializes, directly or as a value of an object literal (const o: Options = { run: async (v) => ... }, also { ... } satisfies Options);
  • the parameter of a project function it is passed to (retry(async () => ...) with retry(task: () => Promise<Result<T, E>>));
  • the declared return type of the function that returns it, directly or as a method or value of the object literal it returns (function fake(): Api { return { async fetch() { ... } } }, also async (): Promise<Api>, and an arrow's expression body).

The types may be type aliases or interfaces in the same file or imported (not generic ones), and the function may be imported. A callback that declares its own return type is checked against that one.

✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

type Command = { run: (text: string) => Result<number, PortError> };

function parsePort(text: string): Result<number, PortError> {
  const port = Number(text);
  return Number.isInteger(port) ? port : err(PortError.NotAPort({ text }));
}

export const portCommand: Command = {
  run: (text) => {
    const port = q(parsePort(text));
    return port + 1;
  },
};

type Api = { fetchPort: (text: string) => Promise<Result<number, PortError>> };

export function fakeApi(): Api {
  return {
    async fetchPort(text) {
      const port = q(parsePort(text));
      return port;
    },
  };
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

type Command = { run: (text: string) => Result<number, PortError> };

function parsePort(text: string): Result<number, PortError> {
  const port = Number(text);
  return Number.isInteger(port) ? port : err(({ tag: "NotAPort", text } satisfies PortError as PortError));
}

export const portCommand: Command = {
  run: (text) => {
    const __q1 = parsePort(text); if (__q1 instanceof __qErr) return __q1; const port = __q1;
    return port + 1;
  },
};

type Api = { fetchPort: (text: string) => Promise<Result<number, PortError>> };

export function fakeApi(): Api {
  return {
    async fetchPort(text) {
      const __q2 = parsePort(text); if (__q2 instanceof __qErr) return __q2; const port = __q2;
      return port;
    },
  };
}

That the error q passes on is one the function declares is q-undeclared-error's question (a type-check of the transformed code, safe-ts emit + tsc, asks it too).

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