forked from syntax-tree/unist-util-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test-d.ts
59 lines (53 loc) · 1.8 KB
/
index.test-d.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {expectType, expectError} from 'tsd'
import {Node} from 'unist'
import {Root, Heading, Paragraph} from 'mdast'
import {filter} from './index.js'
const root: Root = {type: 'root', children: []}
/* eslint-disable @typescript-eslint/consistent-type-assertions */
const justSomeNode = {type: 'whatever'} as Node
const headingOrParagraph = {
type: 'paragraph',
children: []
} as Heading | Paragraph
/* eslint-enable @typescript-eslint/consistent-type-assertions */
expectError(filter())
expectType<Root>(filter(root))
expectType<Root>(filter(root, 'root'))
expectType<Root>(filter(root, {}, 'root'))
expectError(filter(root, {notAnOption: true}, 'root'))
expectType<Root>(filter(root, {cascade: false}, 'root'))
expectType<null>(filter(root, 'heading'))
expectType<null>(filter(root, {cascade: false}, 'notAHeading'))
// Vague types.
expectType<Heading | Paragraph>(filter(headingOrParagraph))
expectType<Paragraph | null>(filter(headingOrParagraph, 'paragraph'))
expectType<null>(filter(headingOrParagraph, 'notAHeading'))
expectType<Heading | null>(
filter(headingOrParagraph, {cascade: false}, 'heading')
)
expectType<Heading | Paragraph | null>(
filter(headingOrParagraph, {cascade: false}, () => Math.random() > 0.5)
)
expectType<Heading | null>(
filter(
headingOrParagraph,
{cascade: false},
(node: Node): node is Heading => node.type === 'heading'
)
)
// Abstract types.
// These don’t work well.
// Use strict nodes types.
expectType<Node>(filter(justSomeNode))
expectType<null>(filter(justSomeNode, '???'))
expectType<null>(filter(justSomeNode, {cascade: false}, '???'))
expectType<Node | null>(
filter(justSomeNode, {cascade: false}, () => Math.random() > 0.5)
)
expectType<null>(
filter(
justSomeNode,
{cascade: false},
(node: Node): node is Heading => node.type === 'heading'
)
)