Skip to content

json-type ​

parseJson<T>, validate<T>, fromFields<T> or arbitrary<T> is used with a type that JSON can't produce, or that SAFE-TS can't resolve. Options for parseJson and validate that aren't an object literal ({ unknownKeys: "error" }) are reported too: the validator is generated for them at build time.

Why: the validator is generated from T at build time. That only works for types that describe JSON data, written out so SAFE-TS can read them. Guide: JSON.

✕ Rejectedjson-type
ts
import { type JsonError, parseJson } from "@evobug_com/safe-ts-std/json";
import type { Result } from "@evobug_com/safe-ts-std/result";

type Account = { readonly id: string; readonly opened: Date; readonly greet: () => string };

export function loadAccount(text: string): Result<Account, JsonError> {
  return parseJson<Account>(text);
}
✓ Accepted0 diagnostics
ts
import { type JsonError, parseJson } from "@evobug_com/safe-ts-std/json";
import type { Result } from "@evobug_com/safe-ts-std/result";

type Account = { readonly id: string; readonly opened: string; readonly tags: readonly string[] };

export function loadAccount(text: string): Result<Account, JsonError> {
  return parseJson<Account>(text);
}
ts
import { parseJsonWith as __jsonParse, validateWith as __jsonValidate, jsonFail as __jsonFail, isJsonErr as __jsonIsErr, isJsonObject as __jsonIsObject, type JsonResult as __JsonResult } from "@evobug_com/safe-ts-std/json"; import { type JsonError, parseJson } from "@evobug_com/safe-ts-std/json";
import type { Result } from "@evobug_com/safe-ts-std/result";

type Account = { readonly id: string; readonly opened: string; readonly tags: readonly string[] };

export function loadAccount(text: string): Result<Account, JsonError> {
  return __jsonParse<Account>(text, __v0);
}
// ---- generated by safe-ts: JSON validators ----
type __T0 = { "id": string; "opened": string; "tags": (string)[]; };
function __va0(v: unknown, p: string): __JsonResult<(string)[]> {
  if (!Array.isArray(v)) return __jsonFail(p, "array", v);
  const out: (string)[] = [];
  for (let __j7 = 0; __j7 < v.length; __j7++) {
  const __j8: unknown = v[__j7];
  if (typeof __j8 !== "string") return __jsonFail(p + "[" + __j7 + "]", "string", __j8);
  const __j9 = __j8;
  out.push(__j9);
  }
  return out;
}
function __v0(v: unknown, p: string): __JsonResult<__T0> {
  if (!__jsonIsObject(v)) return __jsonFail(p, "object", v);
  const __j1 = v["id"];
  if (typeof __j1 !== "string") return __jsonFail(p + ".id", "string", __j1);
  const __j2 = __j1;
  const __j3 = v["opened"];
  if (typeof __j3 !== "string") return __jsonFail(p + ".opened", "string", __j3);
  const __j4 = __j3;
  const __j5 = v["tags"];
  const __j10 = __va0(__j5, p + ".tags");
  if (__jsonIsErr(__j10)) return __j10;
  const __j6 = __j10;
  const out: { "id": string; "opened": string; "tags": (string)[]; } = { "id": __j2, "opened": __j4, "tags": __j6 };
  return out;
}
Instead ofUse
Datean ISO string, converted after validation
anythe actual type of the data, or unknown / JsonValue for any JSON
nullOption<T> or an optional field; a JSON null becomes undefined
Map, Setobjects and arrays
number, symbol or template literal keys (Record<number, V>)string keys (Record<string, V>), converted after validation
listed fields next to an index signaturethe dictionary in a field of its own ({ id: string; extra: Record<string, V> })
a dictionary in a union with another object typethe dictionary in a field, the members told apart by a tag (Enum<...>)
infer inside a union or a generic type, template literal types with string or number in them (outside a conditional type's extends)the resulting type written out, or string
object, other than as a conditional type's test (T extends object)the object type written out, or Record<string, V>
a conditional type the build can't decide (an object against a union of objects, through a field that is a union)the resulting type written out
a type that is never, or generic types nested more than 50 levels (probably endless)the resulting type written out
Pick / Omit of a unionOmit<A, "k"> | Omit<B, "k">
methods, functionsdata only; add behaviour in functions

bigint is supported: it is read from a decimal string, and SafeInt rejects numbers past 2^53 (JSON guide). So are Record<string, V> and index signatures with string keys, for objects whose keys are data (JSON guide), and types computed from others: keyof, T[K], Partial, Required, Pick, Omit, Exclude, Extract, NonNullable, mapped types (with as to rename or drop keys), conditional types with infer in arrays, tuples and fields, T extends object, template literal types of literals and template literal patterns (`${infer N extends number}`), tuple spreads and T["length"] (JSON guide). unknown is any JSON value. Int<Min, Max> is a safe integer in a range. typeof X of a const with a literal value needs no as const, nor does typeof X of Object.freeze(…) of such a value, or of a call of a function whose declared return type gives literal types (a code table built from name lists).

A type imported from library code can't be resolved; copy it into a project type. Library code is what is under a node_modules directory below the project root (the directory safe-ts check runs in, the Vite root, the directory oxlint runs in): a package installed there, or any file there, also one reached by a relative import or an alias. A project that itself lives under some node_modules is still a project when you run in it. A workspace package (linked into node_modules, its source in the project) is project code: its types are found through its types condition or field, as TypeScript finds them. Types imported through a tsconfig.json paths alias (@s/sort), re-exports (export *, export { X } from) and types declared in a function are found too, and so are namespace members (Backend.Models.User, also global ones in declare global { … } or a project declaration file, see namespaces); global types from packages (@types/…) aren't (How imports are followed). The error says whether a name is missing from the module it's imported from, comes from a package, comes from another file under node_modules (naming it), is imported from something that resolves to nothing (an alias missing from paths), or isn't declared at all. The JSON guide lists every supported type.

fromFields<T> accepts an object type, or a union of object types told apart by a field with a literal of its own in each (op, tag); a union without one is reported.

arbitrary<T> accepts the same types, and also reports a type whose every value contains another one (type Loop = { readonly next: Loop }): no value of it can be generated.

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