You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Reacted by William Kelley, g3tr1ght, Rémy Machado, TobiasWenzel, Madyan, Mаартен - Maarten, Dax Agarwal, Sujeet Kc, zjlovezj, pizza-enjoyer and 2 more
With the introduction of Template Literal Types in TypeScript, I believe that this should now be possible to ensure the type-safety.
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.
"it is not type safe", Do you mean there is an error or it is not showing value suggestion?
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.
"it is not type safe", Do you mean there is an error or it is not showing value suggestion?
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 color prop also accept any string like <Typography color="#ff5252" />. I don't see that it is currently possible in typescript right?
I don't see that it is currently possible in typescript right?
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.
importReactfrom'react';typeNamedColor='primary'|'secondary'typeColorVariant='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}`typeHexColorTriplet=stringtypePaletteColor= `#${HexColorTriplet}` | `${NamedColor}.${ColorVariant}`
typeColorProp=PaletteColorfunctionTypography(props: {color: ColorProp}){return<div/>}<><Typographycolor="#f5f5f5"/><Typographycolor="primary.main"/><Typography// @ts-expect-errorcolor="tertiary.main"/></>
I'm picking this up, unless someone else is already on it.
My first step would be autocomplete for dotted theme palette paths (primary.main, text.secondary, common.white, and so on) in Typography's color prop. Custom palette keys added through module augmentation would keep working, and any valid CSS color string would still be accepted, via the string & {} pattern, so existing code doesn't break. I'd add TypeScript declaration tests for the palette paths, augmented palettes, and plain CSS colors.
If that goes well, the same type could be reused for other components' color props in a follow-up.
Activity
siriwatknp commentedon May 31, 2021
"it is not type safe", Do you mean there is an error or it is not showing value suggestion?
eps1lon commentedon May 31, 2021
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.
dstoyanoff commentedon May 31, 2021
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.
siriwatknp commentedon Jun 1, 2021
Agreed, that would be nice to have but from my understanding
colorprop also accept any string like<Typography color="#ff5252" />. I don't see that it is currently possible in typescript right?https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wG4AocmATzCTgAUUAbJGGJAYQmejgF4CYKKBRRqAOhApgAO3xwAPgQDOSNBFkATMZOlz8lSjTpxuvKAxxgBjFmw7m+ylTBGyA5pUwBXWRmBNOAAVWggPKBQwAAtqAAphCDAVAC44AG84DQs0p0trOABfAEoM8jgKuCIYHyhZOAAeLWAANzgAegA+ckLKBu7KxtDICKjYrJ5ofgAiAGJMAFZFxemOgcqG4fDImOoJixnhUXEpGVlVrvIGrqA
eps1lon commentedon Jun 1, 2021
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.
Playground Link
maapteh commentedon Jul 28, 2022
chakra-ui did this
DeviousCardinal00 commentedon Nov 30, 2026
I'm picking this up, unless someone else is already on it.
My first step would be autocomplete for dotted theme palette paths (
primary.main,text.secondary,common.white, and so on) inTypography'scolorprop. Custom palette keys added through module augmentation would keep working, and any valid CSS color string would still be accepted, via thestring & {}pattern, so existing code doesn't break. I'd add TypeScript declaration tests for the palette paths, augmented palettes, and plain CSS colors.If that goes well, the same type could be reused for other components'
colorprops in a follow-up.