JS Type Checker Wish List: Regex validated strings
type HexColor = /#[0-9a-f]+/i;
const color: HexColor = '#eee'; // fine
const color: HexColor = 'red'; // error!
Unclear how that would cover this use case? Imagine you had some CSS-in-JS API where you have your CSS properties typed. How would you define the `color` property without just using `string`?
-
-
You could change your CSS-in-JS library’s API a bit and use opaque types to be fully safe. E.g. instead of colour being a string it has to be an opaque type RGB that can only be created using an rgb function that takes three numbers. + other cases.
-
I don't want runtime validation of the types, and the CIJ library needs all style values to be static.
- 1 more reply
New conversation -
-
-
Oh, I see how this would be better than an opaque type now!
With opaque types you’d need a constructor function like color(string) which would throw at runtime instead of reporting a static error. Presumably you already have that for your CSS-in-JS framework though.Thanks. Twitter will use this to make your timeline better. UndoUndo
-
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 