Skip to content

Commit b44f1e9

Browse files
authored
Add support for a nullish test in types
Reviewed-by: Junyoung Choi <fluke8259@gmail.com> Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com> Closes GH-11.
1 parent bd97123 commit b44f1e9

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ declare namespace unistUtilIs {
3737
*
3838
* @typeParam T type of node that passes test
3939
*/
40-
type Test<T extends Node> = TestType<T> | TestObject<T> | TestFunction<T>
40+
type Test<T extends Node> =
41+
| TestType<T>
42+
| TestObject<T>
43+
| TestFunction<T>
44+
| null
45+
| undefined
4146
}
4247

4348
/**
@@ -57,7 +62,7 @@ declare namespace unistUtilIs {
5762
*/
5863
declare function unistUtilIs<T extends Node>(
5964
node: unknown,
60-
test: unistUtilIs.Test<T> | Array<unistUtilIs.Test<any>>,
65+
test?: unistUtilIs.Test<T> | Array<unistUtilIs.Test<any>>,
6166
index?: number,
6267
parent?: Parent,
6368
context?: any

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
2626
"contributors": [
2727
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
28-
"Christian Murphy <christian.murphy.42@gmail.com>"
28+
"Christian Murphy <christian.murphy.42@gmail.com>",
29+
"Lucas Brandstaetter <lucas@brandstaetter.tech> (https://github.com/Roang-zero1)"
2930
],
3031
"files": [
3132
"index.js",

unist-util-is-test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Node, Parent} from 'unist'
2+
23
import {Heading} from 'mdast'
4+
35
import unified = require('unified')
46
import is = require('unist-util-is')
57
import convert = require('unist-util-is/convert')
@@ -47,8 +49,6 @@ const maybeElement: Element = element
4749
is()
4850
// $ExpectError
4951
is<Node>()
50-
// $ExpectError
51-
is<Node>(heading)
5252

5353
/* Incorrect generic. */
5454
// $ExpectError
@@ -61,6 +61,11 @@ is<Record<string, unknown>>(heading, 'heading')
6161
/* Should be assignable to boolean. */
6262
const wasItAHeading: boolean = is<Heading>(heading, 'heading')
6363

64+
/* Test is optional */
65+
is<Node>(heading)
66+
is<Node>(heading, null)
67+
is<Node>(heading, undefined)
68+
6469
/* Should support string tests. */
6570
is<Heading>(heading, 'heading')
6671
is<Heading>(element, 'heading')
@@ -190,3 +195,5 @@ convert<Element>({type: 'heading', depth: 2})
190195
convert<Heading>(isHeading)
191196
// $ExpectError
192197
convert<Element>(isHeading)
198+
convert<Node>(null)
199+
convert<Node>(undefined)

0 commit comments

Comments
 (0)