Skip to content

Instantly share code, notes, and snippets.

@mikr13
Created May 18, 2025 16:53
Show Gist options
  • Save mikr13/c4a3175b1ec84f7994275d3cd1119872 to your computer and use it in GitHub Desktop.
Save mikr13/c4a3175b1ec84f7994275d3cd1119872 to your computer and use it in GitHub Desktop.
const colorMap = {
red: "#f00",
green: "#0f0",
blue: "#00f",
} as const;
type Color = keyof typeof colorMap;
type LooseAutocomplete<T extends string> = T | (string & {});
function setColor(color: LooseAutocomplete<Color>) {
// accepts "red", "green", "blue" with autocomplete
// also accepts any other string without error
}
// Valid usages
setColor("red"); // ✅ autocomplete
setColor("purple"); // ✅ no autocomplete, but allowed
setColor("green"); // ✅ autocomplete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment