Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You wouldn't use `test()` like in your example.

`test()` only returns a boolean, you want to look at `exec()` which returns the result of the regular expression (typed as: `RegExpExecArray | null` which you can narrow down to `RegExpExecArray` by checking if the result is null or not).

RegExpExecArray gives you a structure that looks different between the jsdoc version and the typescript version.

The typescript version has `.groups` inside RegExpExecArray.

You can use that as is, or you can add some extra type utilities to extract the named groups from inside the regex. (If you look inside typescript's issues on github you'll find a whole bunch of them that people have wanted typescript to include by default).

There's a few regex PRs to add extraction and syntax checking to typescript by default, but they're delayed until after the compiler switch from ts to go. (but there's porting to go in the PRs anyway).





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.


Your examples has groups only typed as { [key: string]: string }.

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.


The ts-regexp package `infer`s out of template literal types to do its magic. That's possible from JSDoc too: https://www.typescriptlang.org/play/?filetype=js#code/PQKhCg...

The behavior of ts-regexp is not especially related to how `RegExpExecArray` is typed. That package uses unsafe assertions to implement its API.

The only thing that requires TypeScript syntax in the playground you linked is the non-null assertions. Here's that same code in JSDoc mode with the non-null assertions removed (`strictNullChecks` is disabled instead): https://www.typescriptlang.org/play/?filetype=js#code/JYWwDg...

---

> 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.

If you can show a concrete example what you want using TypeScript syntax then I'm pretty sure I could port it to JSDoc syntax. (To be clear I'd personally much rather write the TypeScript syntax as I find JSDoc comments annoying & verbose, but they're plenty powerful, being nearly isomorphic to TypeScript annotations.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: