Skip to content

stringMatching

Matcher for a string matching a regular expression.

Matchers are applied through assertObjectMatches, where they narrow the corresponding property type.

A regular expression carries no type-level shape, so the property is narrowed to a string and no further.

import { assertObjectMatches, stringMatching } from "@kensio/smartass";
const value: unknown = {
key: "2026/08/23/14/orders-1-2026-08-23-14-30-00-000",
};
assertObjectMatches(value, {
key: stringMatching(/^\d{4}\/\d{2}\/\d{2}\//),
});
// value is now narrowed to an object with a string key
// {
// key: string;
// }

Source file: src/assert/string-matches/string-matches.match.ts