Skip to main content

Param

The Param interface can be used to implement custom route parameters.

Properties

name

The name of the parameter.

readonly name: Name

exp

The regular expression used to determine whether a segment (or segments) of a path match the parameter.

readonly exp: RegExp
caution

The expression must have exactly one capturing group.

Methods

format

Format an argument for use in a path.

function format(arg: Arg): string;
See also

Parameters

Accepts a single argument - the value to format. The value must have a type matching the Arg type variable.

Return value

A formatted string representing the supplied value.

The strings produced by format should match exp, and the resulting capturing group content should be able to be parsed by parse.

parse

Parse a successful match against exp.

function parse(match: string): Arg;

Parameters

Accepts a single argument - the capturing group content of a successful match against exp.

parse should be capable of parsing any capturing group content that can occur in a successful match of exp against the strings that are produced by format.

Return value

The parsed value of the parameter. The value must have a type matching the Arg type variable.

Type variables

The Param type is a generic type:

Param<Name extends string, Arg = string>
See also

Name

The parameter's name as a string literal type.

Arg

The type of argument the parameter accepts.

Leaving off the Arg type variable creates a Param where the Arg type is string, meaning that the following types are equivalent:

import { Param } from "@snout/router-path";

type ParamB = Param<"param-name">;
type ParamA = Param<"param-name", string>;
See also