Skip to content

Commit 8f9b89b

Browse files
committed
📝 Update README example code
1 parent 5c0aa28 commit 8f9b89b

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

README.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,38 @@ predicate types of `x` more precisely like:
3636
```typescript
3737
import { is } from "./mod.ts";
3838

39-
const a: unknown = ["a", "b", "c"];
40-
41-
if (is.Array(a)) {
42-
// 'a' is 'unknown[]' in this block
43-
}
44-
45-
if (is.ArrayOf(is.String)(a)) {
46-
// 'a' is 'string[]' in this block
39+
const isArticle = is.ObjectOf({
40+
title: is.String,
41+
body: is.String,
42+
refs: is.ArrayOf(is.OneOf([
43+
is.String,
44+
is.ObjectOf({
45+
name: is.String,
46+
url: is.String,
47+
}),
48+
])),
49+
});
50+
51+
const a: unknown = {
52+
title: "Awesome article",
53+
body: "This is an awesome article",
54+
refs: [
55+
{ name: "Deno", url: "https://deno.land/" },
56+
"https://github.com",
57+
],
58+
};
59+
if (isArticle(a)) {
60+
// a is narrowed to the type of `isArticle`
61+
console.log(a.title);
62+
console.log(a.body);
63+
for (const ref of a.refs) {
64+
if (is.String(ref)) {
65+
console.log(ref);
66+
} else {
67+
console.log(ref.name);
68+
console.log(ref.url);
69+
}
70+
}
4771
}
4872
```
4973

0 commit comments

Comments
 (0)