Skip to content

Type safe colors #26475

Open
Open

Description

@dstoyanoff

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:

<Typography color="common.white">text</Typography>

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.

Activity

  1. added
    status: waiting for maintainer
    on May 27, 2021
  2. siriwatknp commented on May 31, 2021

    @siriwatknp
    Member

    "it is not type safe", Do you mean there is an error or it is not showing value suggestion?

  3. added
    type: new feature
    and removed
    status: waiting for maintainer
    on May 31, 2021
  4. eps1lon commented on May 31, 2021

    @eps1lon
    Member

    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.

  5. dstoyanoff commented on May 31, 2021

    @dstoyanoff
    Author

    "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.

  6. siriwatknp commented on Jun 1, 2021

    @siriwatknp
    Member

    "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?

    image

    https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wG4AocmATzCTgAUUAbJGGJAYQmejgF4CYKKBRRqAOhApgAO3xwAPgQDOSNBFkATMZOlz8lSjTpxuvKAxxgBjFmw7m+ylTBGyA5pUwBXWRmBNOAAVWggPKBQwAAtqAAphCDAVAC44AG84DQs0p0trOABfAEoM8jgKuCIYHyhZOAAeLWAANzgAegA+ckLKBu7KxtDICKjYrJ5ofgAiAGJMAFZFxemOgcqG4fDImOoJixnhUXEpGVlVrvIGrqA

  7. eps1lon commented on Jun 1, 2021

    @eps1lon
    Member

    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.

    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" />
    </>

    Playground Link

  8. maapteh commented on Jul 28, 2022

    @maapteh

    chakra-ui did this

  9. added
    scope: all components
    on Aug 16, 2025
  10. DeviousCardinal00 commented on Nov 30, 2026

    @DeviousCardinal00

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope: all componentstype: new featuretypescript

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions