Skip to content

Commit 5a652b8

Browse files
committed
feat: add strong typing for String.matchAll
1 parent c420cd7 commit 5a652b8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ declare global {
2121
match<T extends string>(
2222
regexp: MagicRegExp<T>
2323
): (Omit<RegExpMatchArray, 'groups'> & { groups: Record<T, string | undefined> }) | null
24+
matchAll<T extends string>(
25+
regexp: MagicRegExp<T>
26+
): IterableIterator<
27+
Omit<RegExpMatchArray, 'groups'> & { groups: Record<T, string | undefined> }
28+
>
2429
}
2530
}

test/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, it, describe } from 'vitest'
2+
import { expectTypeOf } from 'expect-type'
23

34
import { anyOf, char, createRegExp, exactly, global } from '../src'
45
import { createInput } from '../src/core/internal'
@@ -70,5 +71,20 @@ describe('inputs', () => {
7071
"test2": "baz",
7172
}
7273
`)
74+
expectTypeOf<Record<'test' | 'test2', string | undefined> | undefined>(
75+
'fobazzer'.match(createRegExp(pattern))?.groups
76+
)
77+
// @ts-expect-error
78+
'fobazzer'.match(createRegExp(pattern))?.groups.other
79+
80+
for (const match of 'fobazzer'.matchAll(createRegExp(pattern, [global]))) {
81+
expect(match.groups).toMatchInlineSnapshot(`
82+
{
83+
"test": undefined,
84+
"test2": "baz",
85+
}
86+
`)
87+
expectTypeOf<Record<'test' | 'test2', string | undefined>>(match.groups)
88+
}
7389
})
7490
})

0 commit comments

Comments
 (0)