News for hackers. Stuff that matters.
Re:TypeScript: Template string types and mapped type 'as' cl... by tengbretson on Tuesday September 01, 2020 @12:40PM (#24343652)
I think the op is asking for actual type refinement by a regex condition.
Re:TypeScript: Template string types and mapped type 'as' cl... by reificator on Tuesday September 01, 2020 @01:03PM (#24343970)
That would make a lot of sense given the topic at hand. Thanks for the clarification.
Re:TypeScript: Template string types and mapped type 'as' cl... by chadlavi on Tuesday September 01, 2020 @01:06PM (#24344007)
especially as a library maintainer, I would really love to be able to say, for example, "you have to pass an email address to this prop" or "you have to pass a US phone number"
Re:TypeScript: Template string types and mapped type 'as' cl... by reificator on Tuesday September 01, 2020 @01:17PM (#24344141)
So you're the one I should blame whenever some regex fails to validate my perfectly standards compliant email address!

I get very frustrated when I sign up for storefronts using an email with a `.dev` address and some wanker decides that I shouldn't be allowed to give them money because my email address doesn't end in `@gmail.com`.

Stop this. Regex is not the right tool for this job. If you're going to use the regex hammer at least use the full standards compliant one shown here: https://stackoverflow.com/a/201378

> The problem with regular expressions is that telling someone that their perfectly valid e-mail address is invalid (a false positive) because your regular expression can't handle it is just rude and impolite from the user's perspective. A state engine for the purpose can both validate and even correct e-mail addresses that would otherwise be considered invalid as it disassembles the e-mail address according to each RFC.

Re:TypeScript: Template string types and mapped type 'as' cl... by spicybright on Tuesday September 01, 2020 @04:31PM (#24346703)
Or if you're even too lazy for that, just match `.+@.+` and handle errors properly if sending an email fails (which you'll obviously have to handle anyways as a valid email address might not be in service)
Re:TypeScript: Template string types and mapped type 'as' cl... by chadlavi on Tuesday September 01, 2020 @02:45PM (#24345295)
No, it's an inner-source library, but thanks for expressing your generalized frustration at me!

But also I would use a standards-compliant regex. I also hate it when people don't implement standards to spec.

Re:TypeScript: Template string types and mapped type 'as' cl... by pseudonom- on Tuesday September 01, 2020 @01:16PM (#24344133)
This can be fairly smoothly achieved already: https://dev.to/gcanti/functional-design-smart-constructors-1...
Re:TypeScript: Template string types and mapped type 'as' cl... by chadlavi on Tuesday September 01, 2020 @02:49PM (#24345355)
Yeah I'm familiar with branded types, but they're cumbersome and require API changes beyond just type narrowing to implement. I just wish they'd figure out how to implement strong types in typescript without js side effects.