-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype-string.match.ts
More file actions
41 lines (40 loc) · 1.06 KB
/
Copy pathtype-string.match.ts
File metadata and controls
41 lines (40 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { createMatcher } from "../../match/match.js";
import {
typeStringMatcher,
type TypeStringMatcher,
} from "./type-string.type.js";
/**
* Matcher for a string value.
* Matchers are applied through assertObjectMatches, where they narrow the
* corresponding property type.
* Type information that already exists in the calling scope is incorporated.
* @example
* ```ts
* import { assertObjectMatches, typeString } from "@kensio/smartass";
*
* const value: unknown = {
* name: "Alice",
* };
*
* assertObjectMatches(value, {
* name: typeString(),
* });
*
* // value is now narrowed to an object with a string name
* // {
* // name: string;
* // }
* ```
*/
export function typeString(): TypeStringMatcher {
return {
...createMatcher(
(value): value is string => typeof value === "string",
() => "string",
() => "String()",
),
// Runtime marker used only to make the matcher type nominal for type-level
// refinement dispatch. It is not part of the user-facing matcher behaviour.
[typeStringMatcher]: true,
};
}