Repository navigation
Type hinting and annotation for tailwind classes in react (jsx,tsx) #20676
Replies: 5 comments
|
tailwind doesn't ship a type for that, there's no enum of all the classes to import. there can't really be one either, arbitrary values make the list endless. you'd just type the array yourself: type BtnClass = "text-lg" | "bg-blue-500" | "px-5" | "py-3" | "text-white" typescript rejects anything not in the union, so a typo fails at compile time. the official tailwind css intellisense extension also flags classes that don't exist while you type, which catches most typos before you even get there. |
|
Hi @arunkumar413! Because Tailwind CSS allows arbitrary values ( However, in production React codebases, here are the two standard patterns to get strong type-hinting, autocompletion, and deduplication for grouped Tailwind classes: Method 1: Class Variance Authority (
|
|
Tailwind classes are plain strings, so TypeScript can't type-check them out of the box, there's no built-in typed className in Tailwind or React. What you can do:
In practice, (1) + (2) is what most React + Tailwind teams use; full type-checking of every class string is usually more trouble than it's worth. |
|
Hi @arunkumar413! There are two main reasons generating a strict TypeScript enum/union for all possible Tailwind CSS classes isn't standard practice: Dynamic / Arbitrary classes: Tailwind utilities support arbitrary values (like bg-[#1da1f2], w-[320px], grid-cols-[1fr_auto]), making the full set of valid classes mathematically infinite. That said, here are the two industry-standard patterns used across the React ecosystem to solve this cleanly:
Instead of raw string arrays, use cva tsx
If you prefer keeping string arrays or helper functions, configure the Tailwind CSS IntelliSense extension in VS Code to autocomplete and lint classes inside custom functions or arrays. Add this to .vscode/settings.json: json Now, any array named *Classes (like primaryBtnClasses) will automatically get Tailwind autocomplete, linting, and error squiggles directly in your editor. Pair this with clsx and tailwind-merge to merge arrays cleanly: tsx If it helped, kindly mark it as answered |
|
Written for: Arun and the team. Hi Arun, Short answer: there's no practical type or enum that covers every valid Tailwind class. The set is effectively unlimited: variants ( Here's what works instead. 1. Lint the classes instead of typing them (recommended). 2. Get autocomplete inside the array. The Tailwind CSS IntelliSense VS Code extension can autocomplete and flag classes outside "tailwindCSS.experimental.classRegex": [
["Classes\\s*=\\s*\\[([^\\]]*)\\]", "[\"'`]([^\"'`]*)[\"'`]"]
]3. If you want a type anyway, keep it to a small allow-list. type Size = "sm" | "lg" | "xl";
type Color = "blue" | "red" | "gray";
type Shade = 100 | 500 | 700;
type TwClass = `text-${Size}` | `bg-${Color}-${Shade}` | `px-${number}` | `py-${number}` | "text-white";
const primaryBtnClasses: TwClass[] = ["text-lg", "bg-blue-500", "px-5", "py-3", "text-white"];Each class family you add has to be maintained by hand. Typos are caught, but anything you haven't listed is rejected even when it's a valid Tailwind class. 4. For the grouping itself, a plain string is enough, since const primaryBtn = "text-lg bg-blue-500 px-5 py-3 text-white";For buttons with variants (primary, secondary, sizes), My suggestion: use a string or cva, plus the ESLint rule and IntelliSense. You get the validation you're after without maintaining a type. Thanks, |
Uh oh!
There was an error while loading. Please reload this page.
Hi Team,
I'm thinking of an idea where tailwind classes related to a component pattern appearing frequently are grouped together in an array as below. This keeps the HTML less polluted in a react component. I would like to know if there is a way to type annotate the array with an enum so that only valid tailwind classes are allowed.
const primaryBtnClasses = ["text-lg", 'bg-blue-500', "px-5", "py-3", "text-white"]<button className={primaryBtnClasses.join(" ")}> Click me </button>Thanks,
Arun
All reactions