Param
The Param
interface can be used to implement custom route parameters.
Properties
name
The name of the parameter.
readonly name: Name
- Type-safe parameter names in the Custom parameters guide
exp
The regular expression used to determine whether a segment (or segments) of a path match the parameter.
readonly exp: RegExp
The expression must have exactly one capturing group.
- Matching and parsing in the Custom parameters guide
Methods
format
Format an argument for use in a path.
function format(arg: Arg): string;
- Formatting in the Custom parameters guide
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;
- Matching and parsing in the Custom parameters guide
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>
- Type safety in the Custom parameters guide
Name
The parameter's name
as a string
literal type.
- Type-safe parameter names in the Custom parameters guide
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>;
- Type safety in the Custom parameters guide