-
-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adjust color type to make it more safe #746
Comments
Maybe we should "just" have colors (string) and platformColors ? That would definitely make things harder to bind, but maybe not that hard & would let some people to have an easy first approach on color (with strings)? |
Or a new |
Just to be sure about the motivation on this change: aren't you supposed to know when you manipulate a color before manipulating it? Or do you have "random" color (string or platformColor) that you put in a registry and you don't know when you are going to lighten or darken one if that's possible or not? With or without types, you are supposed to know upfront anyway right? How is the type system going to help with that, except if you don't really know what you are manipulating ? I am not closed to anything, but I would like to keep something simple for beginner, and I don't really want to maintain 2 Styles modules called "safe and unsafe" just because of colors. We actually have something way more safe that ReactDOM.Style that accepts string in a lot of place where we accept poly variants or value with unit. |
I have a big record that contains the color theme of the app ( Worse than that, right now I know I do some manipulation on some colors of the app, if I would convert some colors to platform colors the app would start crashing with type errors in some places. |
Btw this is very similar to how "margins" are typed currently, where you have to wrap them with |
I totally understand your point. Thing is About your first proposal, not sure if "Color.get"` has some value, as you cannot get the value of a PlatformColor. Sure that would be a breaking change anyway as I don't want us to have a "unsafe" Style vs a "safe" Style module (otherwise we could have literally an untyped Style module like ReactDOM.Style vs "what we have + more strict types for color". I never wrote a codemod, but I think it could be fairly easy to append "->color" to all color style props? |
Yes I think a
It would basically unbox the type, so if you call Color.get on a
Yes makes sense to have a codemod for this one, but it might be tricky because if we do a syntax transform we don't know the types, so we don't know if the |
Ok that's a thing we can do I guess. |
The problem
Now that we have
PlatformColor
(nice implementation btw), I think we should think about color not being a type alias to string anymore (part reversal of this PR). I would like to gradually start to use platform colors in my app, now all colors are strings, and sometimes I do some manipulation on them (like darken/lighten), of course that wouldn't work on platform colors.Considered solution
So to make colors safe I think we should make them phantom typed.
With platform colors being
color<PlatformColor.t>
.In
Style.t
we can accept all colors (we need a lot of type parameters in the external though)Alternative solution
An opaque type. The type of the color would be lost, so we can't easily have a
get
ortoString
that would raise a type error for a platform color. The upside for this is that we need less type parameters.The text was updated successfully, but these errors were encountered: