createError
Creates a native Error with a typed name and custom data. Use it for one-off failures without writing an Error subclass.
Usage
createError(definition)
Every call creates a fresh native error with a stack. message and optional cause keep their native, non-enumerable descriptors. Custom fields are copied shallowly; the definition is not mutated.
Causes
cause accepts any value, just like native ErrorOptions. Omit it when there is no underlying failure.
Explicit output contracts
Pass an error type when the returned value must satisfy an existing contract:
Missing data, incompatible field types, and a different name fail typechecking. Native stack and toString come from Error construction, not from the output contract's input fields. The result is a native Error, not an instance of a custom subclass.
Parameters
An object with required string name and message, optional cause, and any custom data fields. Do not supply stack; each error gets its own native stack.
Returns
An ErrorWith preserving the inferred name and custom fields, or the explicit error type E when called as createError<E>(definition).
The name and custom fields are readonly in TypeScript. This does not freeze the instance or clone nested data. The function constructs errors; it does not validate untrusted data or throw the resulting instance.
See also
defineError— define a reusable constructorErrorWith— describe a native error with custom dataerr— wrap an error in a failed ResulttoError— normalize an unknown value