nationid - v2.2.1
    Preparing search index...

    Function parse

    • Detailed parse with a discriminated ParseResult. Never throws on input errors; instead returns { ok: false, error: { kind, ... } } so callers can branch on the failure mode (use this when you need to show a user-facing error message via nationid/i18n.getErrorMessage).

      Generic over the document type code: passing a literal narrows the returned ParseResult<C> so result.code keeps its literal type. A switch (r.code) after the call sees only C as a reachable value, not the whole 124-member DocumentTypeCode union.

      Type Parameters

      • C extends DocumentTypeCode

        The literal DocumentTypeCode of the document being parsed.

      Parameters

      • code: C

        Document type to parse.

      • input: string

        Raw user input.

      Returns ParseResult<C>

      A discriminated union ParseResult<C>: { ok: true, value } on success, or { ok: false, error } describing why the input was rejected. result.code is narrowed to the literal C.

      if code is not registered (input errors do NOT throw).

      const r = parse("BR_CPF", "111.444.777-00");
      r.code; // "BR_CPF" — literal, not DocumentTypeCode
      if (!r.ok) {
      console.log(r.reason.kind); // "invalid_checksum"
      }