Skip to content
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

Open
dstoyanoff opened this issue May 27, 2021 · 7 comments
Open

Type safe colors #26475

dstoyanoff opened this issue May 27, 2021 · 7 comments
Labels
new feature New feature or request typescript

Comments

@dstoyanoff
Copy link

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.

@dstoyanoff dstoyanoff added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label May 27, 2021
@siriwatknp
Copy link
Member

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

@eps1lon eps1lon added new feature New feature or request typescript and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 31, 2021
@eps1lon
Copy link
Member

eps1lon commented 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.

@dstoyanoff
Copy link
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.

@siriwatknp
Copy link
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

@eps1lon
Copy link
Member

eps1lon commented 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

@Raicuparta
Copy link

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.

@maapteh
Copy link

maapteh commented Jul 28, 2022

chakra-ui did this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request typescript
Projects
None yet
Development

No branches or pull requests

5 participants