-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.test-d.ts
63 lines (57 loc) · 1.92 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
60
61
62
63
import type {Heading, Paragraph, Root} from 'mdast'
import {expectError, expectType} from 'tsd'
import type {Node} from 'unist'
import {filter} from 'unist-util-filter'
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<undefined>(filter(root, 'heading'))
expectType<undefined>(filter(root, {cascade: false}, 'notAHeading'))
// Vague types.
expectType<Heading | Paragraph>(filter(headingOrParagraph))
expectType<Paragraph | undefined>(filter(headingOrParagraph, 'paragraph'))
expectType<undefined>(filter(headingOrParagraph, 'notAHeading'))
expectType<Heading | undefined>(
filter(headingOrParagraph, {cascade: false}, 'heading')
)
expectType<Heading | Paragraph | undefined>(
filter(headingOrParagraph, {cascade: false}, function () {
return Math.random() > 0.5
})
)
expectType<Heading | undefined>(
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<undefined>(filter(justSomeNode, '???'))
expectType<undefined>(filter(justSomeNode, {cascade: false}, '???'))
expectType<Node | undefined>(
filter(justSomeNode, {cascade: false}, function () {
return Math.random() > 0.5
})
)
expectType<undefined>(
filter(
justSomeNode,
{cascade: false},
(node: Node): node is Heading => node.type === 'heading'
)
)