Skip to content

Instantly share code, notes, and snippets.

@GauBen
Created February 22, 2026 18:03
Show Gist options
  • Select an option

  • Save GauBen/744c080ec3a5911946cdfbb7115519ae to your computer and use it in GitHub Desktop.

Select an option

Save GauBen/744c080ec3a5911946cdfbb7115519ae to your computer and use it in GitHub Desktop.
import { type InspectColor, styleText } from "node:util";
type StyleText = ((
text: string | TemplateStringsArray,
...args: unknown[]
) => string) & { [key in InspectColor]: StyleText };
function createStyleText(...styles: InspectColor[]): StyleText {
return new Proxy(
(text: string | TemplateStringsArray, ...args: unknown[]) =>
styleText(
styles,
typeof text === "string" ? text : String.raw({ raw: text }, ...args),
),
{
get(_, prop) {
if (typeof prop !== "string")
throw new TypeError("Property must be a string");
return createStyleText(...styles, prop as InspectColor);
},
},
) as StyleText;
}
const k = createStyleText();
console.log(k.red.underline`This is ${k.yellow`yellow`} text`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment