-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Type safe colors #26475
Comments
"it is not type safe", Do you mean there is an error or it is not showing value suggestion? |
This is definitely the plan. But we have to wait for TypeScript 4.0 to reach end-of-life. Until then we still have to support TypeScript 4.0 and the additional effort of shipping types for multiple TypeScript versions is probably not worth the effort. If people are interested in it, they can upvote the issue. |
Hey, I mean there is no suggestion and/or validation of the value that was passed. This is especially problematic in cases of refactoring, where this cannot be automatically replaced. |
Agreed, that would be nice to have but from my understanding |
It should be with template literal types but we would need to experiment if we can cover every possible color and if that's viable. Better autocomplete would already be a good first step. import React from 'react';
type NamedColor = 'primary' | 'secondary'
type ColorVariant = 'main' | 300 | 400 | 500
// too complex to represent
// type HexNumber = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | number
// type HexColorTriplet = `${HexNumber}${HexNumber}${HexNumber}${HexNumber}${HexNumber}${HexNumber}`
type HexColorTriplet = string
type PaletteColor = `#${HexColorTriplet}` | `${NamedColor}.${ColorVariant}`
type ColorProp = PaletteColor
function Typography(props: { color: ColorProp }) {
return <div />
}
<>
<Typography color="#f5f5f5" />
<Typography color="primary.main" />
<Typography
// @ts-expect-error
color="tertiary.main" />
</> |
Sounds like trying to type every possible css color might be a pretty big task with a lot of gotchas. There must be some other project that did this, but I'm not finding anything. |
chakra-ui did this |
As discussed in #13875, there is currently support for specifying the colors, based on the configuration in the theme in the following format: "common.white". For example:
This works great, but it is not type safe. With the introduction of Template Literal Types in TypeScript, I believe that this should now be possible to ensure the type-safety.
The text was updated successfully, but these errors were encountered: