Skip to content

Instantly share code, notes, and snippets.

@serifcolakel
Created December 14, 2023 21:47
Show Gist options
  • Save serifcolakel/a3a42f9bc0b6d8231b3e9220d092f513 to your computer and use it in GitHub Desktop.
Save serifcolakel/a3a42f9bc0b6d8231b3e9220d092f513 to your computer and use it in GitHub Desktop.
Extracts the color value from a string that follows the format `text-{color}-500`.
const colors = ["text-yellow-500", "text-violet-500", "text-red-500"] as const;
/**
* Extracts the color value from a string that follows the format `text-{color}-500`.
* @template S The input string.
* @param {S} input The input string to extract the color from.
* @returns {ExtractColor<S>} The extracted color value.
*/
type ExtractColor<S extends string> = S extends `text-${infer Color}-500`
? Color
: never;
type Color = ExtractColor<(typeof colors)[number]>;
function getColor(color: Color) {
return color;
}
getColor("red");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment