JS Type Checker Wish List: Regex validated strings
type HexColor = /#[0-9a-f]+/i;
const color: HexColor = '#eee'; // fine
const color: HexColor = 'red'; // error!
-
Show this thread
-
I don't really like using regexes here but it's the most intuitive API for defining a validation rule. Super trivial to implement in a type checker too (if it's written in JS, just delegate to the runtime engine). Could have some way to specify the actual message for errors too.
1 reply 0 retweets 10 likesShow this thread -
-
We have it implemented in a branch but apart from the "make sure you pass in only these strings" case which is really compelling, thinking about how you narrow other strings for validation feels off. It means you have to write the same regex in both expression & type positions.
5 replies 0 retweets 13 likes -
Replying to @drosenwasser @appsforartists and
We also worried about non-regular regexes since you would be one typo away from hanging the language service.
1 reply 0 retweets 4 likes -
Replying to @sanders_n @appsforartists and
Yeah, I once accidentally spawned off over 10 threads by using a regex search in Visual Studio and they lived for over a half hour, so I'm now overly cautious.
1 reply 0 retweets 1 like -
Replying to @drosenwasser @sanders_n and
The type checker I'm working on uses workers, so I could possibly detect it and kill the worker.
1 reply 0 retweets 0 likes -
Replying to @sebmck @sanders_n and
The language server process is always killable, but I think it's still pretty undesirable for something that easy to run into. I could imagine regex types that aren't as powerful as actual regexes.
1 reply 0 retweets 2 likes -
Replying to @drosenwasser @sebmck and
You could do so much with strictly regular regexps. Another bonus: matching is simpler and way more efficient.
2 replies 0 retweets 4 likes
TC39 is in a couple of weeks if you want to draft up a proposal for strict regular regexs in native JS 
-
-
I was just discussing this with Michael Saboff and
@gibson042 at the last TC39 meeting — and@hashseed and@schuay have thought about this a lot, too. Let’s just make it happen!2 replies 0 retweets 7 likes -
Regularer Expressions for stage 1!
1 reply 0 retweets 9 likes - 2 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
he/him 