|
|
|
|
|
by matt_kantor
187 days ago
|
|
Thanks for the reply. > RegExpExecArray gives you a structure that looks different between the jsdoc version and the typescript version. > The typescript version has `.groups` inside RegExpExecArray. The JSDoc version has `.groups` too, and it appears to be typed identically to the TypeScript version given similar configuration. Here's the TypeScript version: https://www.typescriptlang.org/play/?target=5&strictNullChec... And here's the JSDoc version: https://www.typescriptlang.org/play/?target=5&strictNullChec... Could you show me what you mean in the playground? You can change it to JSDoc mode by selecting "JavaScript" in the "Lang" dropdown in the "TS Config" tab. |
|
It isn't narrowed down to the actual named capture groups inside the regex.
For the whole semver example, we'd want something typed as: { major: string, minor: string, patch: string }.
(sidenote for this semver example and not regex in general: they will be strings because a major of "01" and a patch of "3-prerelease" are both valid. You can parse them into ints later depending on what you're actually doing with the semver)
---
For an example of a type narrowed regex in the ts playground, I'll take the lazy route and point you to an example someone else has made:
https://www.typescriptlang.org/play/?ssl=1&ssc=39&pln=1&pc=3...
You can use a utility library (e.g. ts-regexp, arkregex, etc...), you can use one of the utilities that are shared around the typescript spaces online for the one part you need, or you can make your own utility type.
---
For my use-case, I just want to know that I can change either my code or regex and I'll be alerted before runtime or buildtime if I've forgotten to account for something.
To carry on with the semver example, if a fourth piece of data was added to the incoming string (e.g. product name, build timestamp, build variant, etc...) and I forgot to update both the code and the regex together and instead only updated one, I'd want an alert in my IDE to tell me that I forgot to update the other one.