Skip to content
API/ListParam

ListParam

src/types/list-param.type.ts2 public methodstype

One page-URL parameter of a list: the key it is read from, the value used when it is missing or unreadable, and how its value is read and written.

parse and serialize use method syntax on purpose. Method parameters are checked bivariantly, which lets a ListParam<number> stand where a ListParam<unknown> is expected — that is how one list holds params of different types.

Any object of this shape is a param, so an app can write its own:

const sinceParam: ListParam<string | undefined> = {
default: undefined,
key: 'since',
parse: (values) => (values.length === 1 && /^\d{4}-\d{2}-\d{2}$/.test(values[0]) ? values[0] : undefined),
serialize: (value) => (value === undefined ? [] : [value]),
};
Property Type Description
default T The value used when the key is absent, or when parse gives up.
key string The page-URL key, read and written verbatim.
parse(values: readonly string[]): T | undefined

Read the value from every value the URL holds for key, in order.

Never called with an empty array. Returning undefined or null — or throwing — falls back to default.

Parameter Type Description
values readonly string[] The key’s values, as URLSearchParams.getAll() returns them

Returns — The value, or undefined to fall back

serialize(value: T): readonly string[]

The values to write for key.

Parameter Type Description
value T The value in list state

Returns — [] to leave the key out; several values repeat the key