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

Generic typings #16

Open
TheOnlyBeardedBeast opened this issue Nov 9, 2021 · 0 comments
Open

Generic typings #16

TheOnlyBeardedBeast opened this issue Nov 9, 2021 · 0 comments

Comments

@TheOnlyBeardedBeast
Copy link

Hi guys, thanks for the good work.

I noticed that every color has its own type generated while a generic type could be made.

type ColorScale = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
type ColorShadeKeys<T extends string> = `${T}${ColorScale}`;
type ColorShades<T extends string> = Record<ColorShadeKeys<T>, string>;

example usage ColorShades<"slate">

I use these generics in a function to generate right typings for color name transformation. (using same name for a neutral color but using different colors in different themes with stiches)

export const transformColor = <TIn extends string, TOut extends string>(
  colors: ColorShades<TIn>,
  from: TIn,
  to: TOut
) =>
  Object.entries(colors).reduce((acc, [key, value]) => {
    acc[key.replace(from, to)] = value as string;
    return acc;
  }, {} as ColorShades<TOut>);

example usage transformColor(slateDark, 'slate', 'neutral')

Not sure if all this helps anyway, but it could reduce the generated typing file sizes, at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant