Created
December 14, 2023 21:47
-
-
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`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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