-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.test-d.ts
264 lines (202 loc) · 5.96 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import type {Content, Heading, Paragraph, Root} from 'mdast'
import {expectAssignable, expectNotType, expectType} from 'tsd'
import type {Node, Parent} from 'unist'
import {convert, is} from 'unist-util-is'
// # Setup
const mdastNode = (function (): Root | Content {
return {type: 'paragraph', children: []}
})()
const unknownValue = (function (): unknown {
return {type: 'something'}
})()
// # `is`
// No node.
expectType<false>(is())
// No test.
expectType<boolean>(is(mdastNode))
if (is(unknownValue)) {
expectType<Node>(unknownValue)
}
/* Nullish test. */
expectType<boolean>(is(mdastNode, null))
expectType<boolean>(is(mdastNode, undefined))
// String test.
if (is(mdastNode, 'heading')) {
console.log('??', mdastNode)
expectType<Heading>(mdastNode)
}
if (is(mdastNode, 'paragraph')) {
expectType<Paragraph>(mdastNode)
}
// Object test.
expectType<boolean>(is(mdastNode, {type: 'heading', depth: 2}))
if (is(mdastNode, {type: 'heading', depth: 2})) {
expectType<Heading>(mdastNode)
}
// TS makes this `type: string`.
if (is(mdastNode, {type: 'paragraph'})) {
expectNotType<Heading>(mdastNode)
}
if (is(mdastNode, {type: 'paragraph'} as const)) {
expectType<Paragraph>(mdastNode)
}
if (is(mdastNode, {type: 'heading', depth: 2})) {
expectType<Heading>(mdastNode)
expectNotType<2>(mdastNode.depth) // TS can’t narrow this normally.
}
if (is(mdastNode, {type: 'heading', depth: 2} as const)) {
expectAssignable<Heading>(mdastNode)
expectType<2>(mdastNode.depth)
}
// Function test (with explicit assertion).
expectType<boolean>(is(mdastNode, isHeading))
if (is(mdastNode, isHeading)) {
expectType<Heading>(mdastNode)
}
if (is(mdastNode, isParagraph)) {
expectType<Paragraph>(mdastNode)
}
if (is(mdastNode, isParagraph)) {
expectNotType<Heading>(mdastNode)
}
if (is(mdastNode, isHeading2)) {
expectAssignable<Heading>(mdastNode)
expectType<2>(mdastNode.depth)
}
// Function test (implicit assertion).
expectType<boolean>(is(mdastNode, isHeadingLoose))
if (is(mdastNode, isHeadingLoose)) {
expectNotType<Heading>(mdastNode)
}
if (is(mdastNode, isParagraphLoose)) {
expectNotType<Heading>(mdastNode)
}
if (is(mdastNode, isHead)) {
expectNotType<Heading>(mdastNode)
}
// Array tests.
// Can’t narrow down.
expectType<boolean>(
is(mdastNode, ['heading', isHeading, isHeadingLoose, {type: 'heading'}])
)
// Can’t narrow down.
if (is(mdastNode, ['heading', isHeading, isHeadingLoose, {type: 'heading'}])) {
expectNotType<Heading>(mdastNode)
}
// # `check`
// No node.
const checkNone = convert()
expectType<boolean>(checkNone())
// No test.
expectType<boolean>(checkNone(mdastNode))
if (checkNone(unknownValue)) {
expectType<Node>(unknownValue)
}
/* Nullish test. */
const checkNull = convert(null)
const checkUndefined = convert(null)
expectType<boolean>(checkNull(mdastNode))
expectType<boolean>(checkUndefined(mdastNode))
// String test.
const checkHeading = convert('heading')
const checkParagraph = convert('paragraph')
if (checkHeading(mdastNode)) {
expectType<Heading>(mdastNode)
}
if (checkParagraph(mdastNode)) {
expectType<Paragraph>(mdastNode)
}
// Object test.
expectType<boolean>(is(mdastNode, {type: 'heading', depth: 2}))
if (is(mdastNode, {type: 'heading', depth: 2})) {
expectType<Heading>(mdastNode)
}
// TS makes this `type: string`.
const checkParagraphProperties = convert({type: 'paragraph'})
const checkParagraphPropertiesConst = convert({type: 'paragraph'} as const)
const checkHeading2Properties = convert({type: 'heading', depth: 2})
const checkHeading2PropertiesConst = convert({
type: 'heading',
depth: 2
} as const)
if (checkParagraphProperties(mdastNode)) {
expectNotType<Paragraph>(mdastNode)
}
if (checkParagraphPropertiesConst(mdastNode)) {
expectType<Paragraph>(mdastNode)
}
if (checkHeading2Properties(mdastNode)) {
expectType<Heading>(mdastNode)
expectNotType<2>(mdastNode.depth) // TS can’t narrow this normally.
}
if (checkHeading2PropertiesConst(mdastNode)) {
expectAssignable<Heading>(mdastNode)
expectType<2>(mdastNode.depth)
}
// Function test (with explicit assertion).
const checkHeadingFunction = convert(isHeading)
const checkParagraphFunction = convert(isParagraph)
const checkHeading2Function = convert(isHeading2)
expectType<boolean>(checkHeadingFunction(mdastNode))
if (checkHeadingFunction(mdastNode)) {
expectType<Heading>(mdastNode)
}
if (checkParagraphFunction(mdastNode)) {
expectType<Paragraph>(mdastNode)
}
if (checkParagraphFunction(mdastNode)) {
expectNotType<Heading>(mdastNode)
}
if (checkHeading2Function(mdastNode)) {
expectAssignable<Heading>(mdastNode)
expectType<2>(mdastNode.depth)
}
// Function test (implicit assertion).
const checkHeadingLooseFunction = convert(isHeadingLoose)
const checkParagraphLooseFunction = convert(isParagraphLoose)
const checkHeadFunction = convert(isHead)
expectType<boolean>(checkHeadingLooseFunction(mdastNode))
if (checkHeadingLooseFunction(mdastNode)) {
expectNotType<Heading>(mdastNode)
}
if (checkParagraphLooseFunction(mdastNode)) {
expectNotType<Heading>(mdastNode)
}
if (checkHeadFunction(mdastNode)) {
expectNotType<Heading>(mdastNode)
}
// Array tests.
// Can’t narrow down.
const isHeadingArray = convert([
'heading',
isHeading,
isHeadingLoose,
{type: 'heading'}
])
expectType<boolean>(isHeadingArray(mdastNode))
// Can’t narrow down.
if (isHeadingArray(mdastNode)) {
expectNotType<Heading>(mdastNode)
}
function isHeading(node: Node): node is Heading {
return node ? node.type === 'heading' : false
}
function isHeading2(node: Node): node is Heading & {depth: 2} {
return isHeading(node) && node.depth === 2
}
function isHeadingLoose(node: Node) {
return node ? node.type === 'heading' : false
}
function isParagraph(node: Node): node is Paragraph {
return node ? node.type === 'paragraph' : false
}
function isParagraphLoose(node: Node) {
return node ? node.type === 'paragraph' : false
}
function isHead(
node: Node,
index: number | undefined,
parent: Parent | undefined
) {
return parent ? index === 0 : false
}