New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
String#matchAll should return iterable of RegExpExecArray instead of RegExpMatchArray #36788
Comments
We don't have any mechanism for carrying around the flags of a RegExp, so the current definitions err on the side of making you assert that those properties will be there in cases where you know that the regexp is in fact global. |
I believe the global flag changing the return value only applies to |
TypeScript doesn't need to inspect or keep track of flags in this case. A regex given to I think the team's decision for how to type |
According to the spec, So, OP is correct, the return type of |
Any updates on this? |
This is still an issue in 2023 |
We still don't have a way to fix it! |
@RyanCavanaugh Sure, but why have you closed the issue? Just because "the mechanism isn't there" doesn't mean the problem is fixed |
There's an infinite number of things that we don't have the mechanics to solve. Issues are open if they are actionable; this is the definition we use |
Untrue. TypeScript/src/lib/es2020.string.d.ts Line 9 in 1481750
Simply changing this line to matchAll(regexp: RegExp): IterableIterator<RegExpExecArray>; will fix this.
|
Can this be re-opened? This was closed by mistake. This is actionable and was never blocked on there being a way to fix it (because there always has been). |
It seems like there was a misreading here - there has been a longstanding issue around typing At least that's what we've gathered revisiting this. |
@DanielRosenwasser yes, exactly! While it would certainly be a welcome improvement for TypeScript to reject non-global regexps being passed to This issue is strictly about the return type of TypeScript can safely assume that |
TypeScript Version: 3.7 (issue is with lib typing)
Search Terms: regex, matchall, es2020
Code
Currently
matchAll
returns an iterable ofRegExpMatchArray
. However, theindex
andinput
properties onRegExpMatchArray
are optional becauseString#match
returns a match object without those if the regex has the global flag (#35157).matchAll
on the other hand doesn't have the same behavior, so it should useRegExpExecArray
where both of those properties are non-optional (or maybe create aRegExpMatchAllArray
type)Expected behavior:
input
andindex
on the match objects are not optionalActual behavior:
TS reports that
input
andindex
may be optionalPlayground Link: (Can't use playground since lib cannot be configured)
Related Issues: #30936 introduced
String#matchAll
The text was updated successfully, but these errors were encountered: