interface / @typed/router/Parser

Parser.Optional

Returns undefined and the original input when its parser fails.

Package version
1.0.0-beta.13
Category
Parser composition
Since
1.0.0
Member of
Parser

Import

import { Parser } from "@typed/router/Parser";

Access Parser.Optional through the imported Parser export. Its declaration below describes the member.

Signatures

interface Optional<P extends Any> extends Parser<unknown> {
    readonly return: InputOf<this> extends infer Input extends string ? Apply<P, Input> extends infer R ? [
        R
    ] extends [
        never
    ] ? readonly [
        undefined,
        Input
    ] : R extends readonly [
        infer Value,
        infer Rest extends string
    ] ? readonly [
        Value,
        Rest
    ] : never : never : never;
}

Why

Optional grammar does not consume input on the absent branch.

Ownership and lifetime

TypeScript instantiates Optional while parsing a string-literal input. Its present or absent result and unchanged failure remainder exist only in the resulting type.

Property: return

Computes this type lambda’s parse result for its current input.

Property: return: Why

The local type-function protocol evaluates each parser combinator through this member without a parallel runtime object.

Property: return: Ownership and lifetime

TypeScript evaluates Optional.return when the surrounding parser is applied; the associated result is erased and retains no input at runtime.

Source