-
Notifications
You must be signed in to change notification settings - Fork 12.8k
RegExMatchArray.groups does not allow an undefined value #61476
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Casting is allowed when you use a compatible type: |
I get why it happens, but it's incorrect from both standpoints (the value can be My issue here is TypeScript reporting the captured group type invalidly (ie; asserting that |
But it isn't a subset, they're incompatible types. |
You're speaking of the types in play here; I'm trying to say that TypeScript's provided type here is incorrect in the first place (and that the correct type would let this code work). Regardless, it seems I'd missed the closed issues that @jcalz mentioned and so this probably won't go anywhere. Thanks! |
.. which would first require #32098, as mentioned. |
(32098 deals with inferring the types from the given RegEx, which is something else entirely) |
🔎 Search Terms
"regexp", "groups", "capture groups"
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?target=99&ts=5.9.0-dev.20250324#code/MYewdgzgLgBAtgQysAFjAvDA5ArA6RZFACgHpiB+AHgBsBTKKOgJwD4BtBAWgC8BdAJQAfSlTABXOACMWrADoATAaQEBuAFABLAGYxihVBTwBzZiHEAHCAJgBvdTEcxQkWKfNWM8JKhNnLEDAIgbYw9IwsAFww0MyaYMaqMBLSUTDiYAp02vF0CjAAvjBCdmEMTMzRGVk5YHlJKTKVMVBxCYVJpKTOCGBYsAqazHTAUDQAnjBQ4xYjwVAOTi4QIPR4NCDGxO4BaouOOnpYjSxYMPHeRH4e1s7gK2sbWwYo1wF4J8zrdAlQKGowLrOZjBFB0CDqApAA
💻 Code
🙁 Actual behavior
{ [key: string]: string }
instead of potentially{ [key: string]: string | undefined }
, and does not allow typecasting it to an object withundefined
as a value.Match.groups
as an object whose keys may be undefined (eg:{ group1: string; group2: undefined }
)in
operator to narrow the type due toundefined
not being a valid value, despite unmatched named groups in alternation in RegEx returning undefined.🙂 Expected behavior
undefined
should be allowed in the value type (for typecasting correctly or for correctly not narrowing within
).Additional information about the issue
Checked #32098 but that looks at inferring types, not allowing casting.
The text was updated successfully, but these errors were encountered: