Skip to content

Commit 579ffbc

Browse files
committed
Fix type support for readonly arrays
Related-to: #17.
1 parent 6db422a commit 579ffbc

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

index.test-d.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
Strong,
2020
TableCell
2121
} from 'mdast'
22-
import type {Node, Parent} from 'unist'
22+
import type {Node} from 'unist'
2323
import {CONTINUE, EXIT, SKIP, visitParents} from './index.js'
2424

2525
/* Setup */
@@ -122,20 +122,21 @@ visitParents(sampleTree, isHeading2, function (node) {
122122
})
123123

124124
// ## Combined tests
125+
// No `as const` fails.
125126
visitParents(sampleTree, ['heading', {depth: 1}, isHeading], function (node) {
126127
// Unfortunately TS casts things in arrays too vague.
127128
expectType<Root | RootContent>(node)
128129
})
129130

130-
// To do: update to `unist-util-is` should make this work?
131-
// visitParents(
132-
// sampleTree,
133-
// ['heading', {depth: 1}, isHeading] as const,
134-
// function (node) {
135-
// // Unfortunately TS casts things in arrays too vague.
136-
// expectType<Root | RootContent>(node)
137-
// }
138-
// )
131+
// As const works.
132+
visitParents(
133+
sampleTree,
134+
['heading', {depth: 1}, isHeading] as const,
135+
function (node) {
136+
// Unfortunately TS casts things in arrays too vague.
137+
expectType<Heading>(node)
138+
}
139+
)
139140

140141
// ## Return type: incorrect.
141142
// @ts-expect-error: not an action.

lib/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
2-
* @typedef {import('unist').Node} UnistNode
3-
* @typedef {import('unist').Parent} UnistParent
2+
* @import {Node as UnistNode, Parent as UnistParent} from 'unist'
43
*/
54

65
/**
@@ -48,8 +47,10 @@
4847

4948
/**
5049
* @typedef {(
51-
* Check extends Array<any>
52-
* ? MatchesOne<Value, Check[keyof Check]>
50+
* Check extends ReadonlyArray<infer T>
51+
* ? MatchesOne<Value, T>
52+
* : Check extends Array<infer T>
53+
* ? MatchesOne<Value, T>
5354
* : MatchesOne<Value, Check>
5455
* )} Matches
5556
* Check whether a node matches a check in the type system.

0 commit comments

Comments
 (0)