Skip to content

Commit 2211a83

Browse files
authored
feat(types): respect global flag for .match and .matchAll types (#29)
1 parent 545d725 commit 2211a83

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,29 @@ export * from './core/types/magic-regexp'
2424
// Add additional overload to global String object types to allow for typed capturing groups
2525
declare global {
2626
interface String {
27-
match<R extends MagicRegExp<string, string, string>>(regexp: R): MagicRegExpMatchArray<R> | null
27+
match<R extends MagicRegExp<string, string, Exclude<Flag, 'g'>>>(
28+
regexp: R
29+
): MagicRegExpMatchArray<R> | null
30+
match<R extends MagicRegExp<string, string, 'g'>>(regexp: R): string[] | null
31+
32+
/** @deprecated String.matchAll requires global flag to be set. */
33+
matchAll<R extends MagicRegExp<string, string, never>>(regexp: R): never
34+
/** @deprecated String.matchAll requires global flag to be set. */
35+
matchAll<R extends MagicRegExp<string, string, Exclude<Flag, 'g'>>>(regexp: R): never
2836

2937
matchAll<R extends MagicRegExp<string, string, string>>(
3038
regexp: R
3139
): IterableIterator<MagicRegExpMatchArray<R>>
40+
41+
/** @deprecated String.replaceAll requires global flag to be set. */
42+
replaceAll<R extends MagicRegExp<string, string, never>>(
43+
searchValue: R,
44+
replaceValue: string | ((substring: string, ...args: any[]) => string)
45+
): never
46+
/** @deprecated String.replaceAll requires global flag to be set. */
47+
replaceAll<R extends MagicRegExp<string, string, Exclude<Flag, 'g'>>>(
48+
searchValue: R,
49+
replaceValue: string | ((substring: string, ...args: any[]) => string)
50+
): never
3251
}
3352
}

test/augments.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,30 @@ describe('String', () => {
1515
it('.match global', () => {
1616
const result = 'test'.match(createRegExp(char.as('foo'), [global]))
1717
expect(Array.isArray(result)).toBeTruthy()
18+
// @ts-expect-error
1819
expect(result?.groups).toBeUndefined()
19-
// TODO: https://github.com/danielroe/magic-regexp/issues/26
20-
// expectTypeOf(result).toEqualTypeOf<null | string[]>()
20+
expectTypeOf(result).toEqualTypeOf<null | string[]>()
2121
})
2222
it.todo('.matchAll non-global', () => {
23-
// TODO: @ts-expect-error
24-
'test'.matchAll(createRegExp(char.as('foo')))
23+
// should be deprecated
24+
expectTypeOf('test'.matchAll(createRegExp(char.as('foo')))).toEqualTypeOf<never>()
25+
expectTypeOf('test'.matchAll(createRegExp(char.as('foo'), ['m']))).toEqualTypeOf<never>()
2526
})
2627
it('.matchAll global', () => {
2728
const results = 'test'.matchAll(createRegExp(char.as('foo'), [global]))
28-
expect(Array.isArray([...results])).toBeTruthy()
29+
let count = 0
2930
for (const result of results) {
30-
expect(result?.groups).toBeUndefined()
31-
// TODO: https://github.com/danielroe/magic-regexp/issues/26
32-
// expectTypeOf(result).toEqualTypeOf<null | string[]>()
31+
count++
32+
expect([...'test'].includes(result?.groups.foo || '')).toBeTruthy()
33+
expectTypeOf(result).toEqualTypeOf<
34+
MagicRegExpMatchArray<MagicRegExp<'/(?<foo>.)/g', 'foo', 'g'>>
35+
>()
3336
}
37+
expect(count).toBe(4)
38+
})
39+
it.todo('.replaceAll non-global', () => {
40+
// should be deprecated
41+
expectTypeOf('test'.replaceAll(createRegExp(char.as('foo')), '')).toEqualTypeOf<never>()
42+
expectTypeOf('test'.replaceAll(createRegExp(char.as('foo'), ['m']), '')).toEqualTypeOf<never>()
3443
})
3544
})

0 commit comments

Comments
 (0)