-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
198 lines (148 loc) · 4.81 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type {Element, Parents, Root, RootContent} from 'hast'
import {expectAssignable, expectNotType, expectType} from 'tsd'
import {convertElement, isElement} from './index.js'
// # Setup
const hastElement = (function (): Root | RootContent {
return {type: 'element', tagName: 'a', properties: {}, children: []}
})()
const unknownValue = (function (): unknown {
return {type: 'something'}
})()
const someTagName = (function (): string {
return 'c'
})()
// # `isElement`
// No node.
expectType<false>(isElement())
// No test.
expectType<boolean>(isElement(hastElement))
if (isElement(unknownValue)) {
expectType<Element>(unknownValue)
}
/* Nullish test. */
expectType<boolean>(isElement(hastElement, null))
expectType<boolean>(isElement(hastElement, undefined))
// String test.
if (isElement(hastElement, 'a')) {
expectType<Element & {tagName: 'a'}>(hastElement)
}
if (isElement(hastElement, 'b')) {
expectType<Element & {tagName: 'b'}>(hastElement)
}
if (isElement(hastElement, someTagName)) {
expectType<Element>(hastElement)
}
// Function test (with explicit assertion).
expectType<boolean>(isElement(hastElement, isHeading2))
if (isElement(hastElement, isHeading2)) {
expectType<Element & {tagName: 'h2'}>(hastElement)
}
if (isElement(hastElement, isParagraph)) {
expectType<Element & {tagName: 'p'}>(hastElement)
}
if (isElement(hastElement, isParagraph)) {
expectNotType<Element & {tagName: 'h2'}>(hastElement)
}
if (isElement(hastElement, isHeading2)) {
expectAssignable<Element>(hastElement)
expectType<'h2'>(hastElement.tagName)
}
// Function test (implicit assertion).
expectType<boolean>(isElement(hastElement, isHeading2Loose))
if (isElement(hastElement, isHeading2Loose)) {
expectNotType<Element & {tagName: 'h2'}>(hastElement)
}
if (isElement(hastElement, isParagraphLoose)) {
expectNotType<Element & {tagName: 'p'}>(hastElement)
}
if (isElement(hastElement, isHead)) {
expectType<Element>(hastElement)
}
// Array tests.
// Can’t narrow down.
expectType<boolean>(isElement(hastElement, ['h2', isHeading2, isHeading2Loose]))
// Can’t narrow down.
if (isElement(hastElement, ['h2', isHeading2, isHeading2Loose])) {
expectNotType<Element & {tagName: 'h2'}>(hastElement)
}
// # `convertElement`
// No node.
const checkNone = convertElement()
expectType<boolean>(checkNone())
// No test.
expectType<boolean>(checkNone(hastElement))
if (checkNone(unknownValue)) {
expectType<Element>(unknownValue)
}
/* Nullish test. */
const checkNull = convertElement(null)
const checkUndefined = convertElement(null)
expectType<boolean>(checkNull(hastElement))
expectType<boolean>(checkUndefined(hastElement))
// String test.
const checkHeading2 = convertElement('h2')
const checkParagraph = convertElement('p')
if (checkHeading2(hastElement)) {
expectType<Element & {tagName: 'h2'}>(hastElement)
}
if (checkParagraph(hastElement)) {
expectType<Element & {tagName: 'p'}>(hastElement)
}
// Function test (with explicit assertion).
const checkParagraphFn = convertElement(isParagraph)
const checkHeading2Fn = convertElement(isHeading2)
expectType<boolean>(checkHeading2Fn(hastElement))
if (checkHeading2Fn(hastElement)) {
expectType<Element & {tagName: 'h2'}>(hastElement)
}
if (checkParagraphFn(hastElement)) {
expectType<Element & {tagName: 'p'}>(hastElement)
}
if (checkParagraphFn(hastElement)) {
expectNotType<Element & {tagName: 'h2'}>(hastElement)
}
if (checkHeading2Fn(hastElement)) {
expectAssignable<Element>(hastElement)
expectType<'h2'>(hastElement.tagName)
}
// Function test (implicit assertion).
const checkHeading2LooseFn = convertElement(isHeading2Loose)
const checkParagraphLooseFn = convertElement(isParagraphLoose)
const checkHeadFn = convertElement(isHead)
expectType<boolean>(checkHeading2LooseFn(hastElement))
if (checkHeading2LooseFn(hastElement)) {
expectNotType<Element & {tagName: 'h2'}>(hastElement)
}
if (checkParagraphLooseFn(hastElement)) {
expectNotType<Element & {tagName: 'p'}>(hastElement)
}
if (checkHeadFn(hastElement)) {
expectNotType<Element & {tagName: 'h2'}>(hastElement)
}
// Array tests.
// Can’t narrow down.
const isHeadingArray = convertElement(['h2', isHeading2, isHeading2Loose])
expectType<boolean>(isHeadingArray(hastElement))
// Can’t narrow down.
if (isHeadingArray(hastElement)) {
expectNotType<Element & {tagName: 'h2'}>(hastElement)
}
function isHeading2(element: Element): element is Element & {tagName: 'h2'} {
return element.tagName === 'h2'
}
function isHeading2Loose(element: Element) {
return element.tagName === 'h2'
}
function isParagraph(element: Element): element is Element & {tagName: 'p'} {
return element.tagName === 'p'
}
function isParagraphLoose(element: Element) {
return element.tagName === 'p'
}
function isHead(
element: Element,
index: number | undefined,
parent: Parents | undefined
) {
return parent ? index === 0 : false
}