question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Jun 1, 2021

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

1reaction
eps1loncommented, May 31, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Web Safe Colors Chart - RapidTables.com
Web safe colors are formed by the combination of red, green and blue (RGB) hex values of 0,33H,66H,99H,CCH,FFH. This results with 6*6*6=216 colors....
Read more >
Type safe colors · Issue #26475 · mui/material-ui - GitHub
As discussed in #13875, there is currently support for specifying the colors, based on the configuration in the theme in the following ...
Read more >
Using Web Colors In 2020 - A Guide [List Of Web-Safe ... - HTML
Color brings the internet to life and enhances the way in which we view the web. But how do you create colors in...
Read more >
Web colors - Wikipedia
Basic colorsEdit ; Hex (RGB) ; Red (RGB) ; Green (RGB) ; Blue (RGB) ; Hue (HSL/HSV) ...
Read more >
HTML Ref » Reference » Appendix E » Browser Safe Colors
FFFFFF R: 255. G: 255. B: 255 FFFFCC R: 255. G: 255. B: 204 FFFF99 R: 255. G: 255. B: 1... FFCC99 R: 255....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found