Skip to content

Instantly share code, notes, and snippets.

@serifcolakel
Last active January 8, 2024 19:23
Show Gist options
  • Save serifcolakel/825423caa9e37a515ad22b355cc55414 to your computer and use it in GitHub Desktop.
Save serifcolakel/825423caa9e37a515ad22b355cc55414 to your computer and use it in GitHub Desktop.
/**
* @description This is a test for the `InferValueFromColor` type.
* @example
* type Color = '#fff';
* type Value = InferValueFromColor<Color>; // Value is 'fff'
* @example
* type Color = '#fff' | '#000';
* type Value = InferValueFromColor<Color>; // Value is 'fff' | '000'
* @example
* type Color = '#fff' | '#000' | '#aaa';
* type Value = InferValueFromColor<Color>; // Value is 'fff' | '000' | 'aaa'
*/
type InferValueFromColor<Color extends string> = Color extends `#${infer Value}` ? `#${Value}` : never;
/**
* @description This is a test for tailwind color the `InferValueFromTWColor` type.
* @example
* type Color = 'bg-white';
* type Value = InferValueFromTWColor<Color>; // Value is never
* @example
* type Color = 'bg-white-100';
* type Value = InferValueFromTWColor<Color>; // Value is { nameSpace: 'bg', color: 'white', tone: '100' }
*/
type InferValueFromTWColor<Color extends string> = Color extends `${infer N}-${infer C}-${infer T}` ? {
nameSpace: N;
color: C;
tone: T;
} : never;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment