Skip to content
i18n-error-base
English
Esc
↑↓navigate↵open⌘Jpreview
On this page

API Reference

Specifications for I18nErrorBase, setErrorMessage, and related types.

I18nErrorBase

A base class for extending Error. message resolves according to lang.

import { I18nErrorBase } from "i18n-error-base";

class MyError extends I18nErrorBase<{ code: string }> {}

Type parameters

Prop Type Default Description
TMeta ErrorMeta | undefined ErrorMeta | undefined The type of meta. Specify undefined when unused.

Static properties

Prop Type Default Description
prefix? string undefined Prepended to the resolved message.

Instance properties

Prop Type Description
meta TMeta Constructor auxiliary info. Readable and writable.
message string Resolves according to lang. Enumerable.
cause unknown The cause from ErrorOptions. A standard Error property.

Constructor

When TMeta excludes undefined, meta is required.

new MyError(meta, message, options?)

When TMeta is undefined, meta can be omitted.

new MyError(message, options?)
new MyError(meta, message, options?)
Parameter Type Description
meta TMeta Auxiliary info for message generation.
message string | MessageFactory<TMeta> Default message for unregistered languages. Functions receive meta.
options ErrorOptions Options compatible with Error’s second argument (e.g. { cause }).

Resolution order

  1. Get lang from getGlobalConfig() ("en" when unset).
  2. Run the registered function for that class and language with meta.
  3. When unregistered, use the default message.
  4. If static prefix is set, prepend it.

setErrorMessage

Registers per-language message factory functions.

import { setErrorMessage } from "i18n-error-base";

setErrorMessage(MyError, (meta) => `...: ${meta.code}`, "ja");
Parameter Type Description
reference I18nErrorBaseConstructor A class extending I18nErrorBase. Non-subclasses are a type error.
message string | (meta) => string The message to register. meta is inferred from the target class.
lang string | Iterable<string> The target language. A single code or an iterable (e.g. an array).

Re-registering the same class and language overwrites.

Types

ErrorMeta

type ErrorMeta = {
  readonly [prop: string]: unknown;
};

The default constraint for TMeta. A readonly object with arbitrary properties.

ErrorOptions

type ErrorOptions =
  ConstructorParameters<typeof Error> extends [
    message?: unknown,
    options?: (infer TOptions) | undefined,
  ]
    ? TOptions
    : { readonly cause?: unknown };

A type derived from Error’s second argument. You can pass cause.

MessageFactory<TMeta>

interface MessageFactory<TMeta extends ErrorMeta | undefined = ErrorMeta | undefined> {
  (meta: TMeta): string;
}

A function that creates a message from meta. Used for both default and registered messages.

I18nErrorBaseParams<TMeta>

Constructor argument overloads.

type I18nErrorBaseParams<TMeta extends ErrorMeta | undefined = ErrorMeta | undefined> =
  [TMeta] extends [undefined]
    ?
        | [message: string | MessageFactory<TMeta>, options?: ErrorOptions | undefined]
        | [meta: undefined, message: string | MessageFactory<TMeta>, options?: ErrorOptions | undefined]
    : [meta: TMeta, message: string | MessageFactory<TMeta>, options?: ErrorOptions | undefined];

Omit meta only when TMeta is undefined.

I18nErrorBaseConstructor

interface I18nErrorBaseConstructor {
  new (...args: any): I18nErrorBase;
}

The type of setErrorMessage’s first argument.

Notes

  • The default lang is "en". Switch with setGlobalConfig({ lang }).
  • No language code normalization or fallback chain (e.g. en-US → en). Unregistered languages use the default message.

Was this page helpful?