-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tailwind 3 broken TS types for config #6422
Comments
Tailwind doesn't come with types, so the issue wouldn't be here. If you're using |
@sachinraja Got it, thank you for clarification. |
I'm not a maintainer, but it looks like there's a discussion here about it |
@sachinraja thanks for the link. It's quite strange that such popular lib doesn't have own integrated TS typings yet. |
Even with the new Tailwind 3.1 release that includes types, I'm still having this issue. I had to rename any files that use |
Thanks @Misha-Front that fixed worked. In the meantime I recommend reopening this issue so the missing types can be provided. |
there is a more convenient solution than using any const fullConfig = resolveConfig(tailwindConfig);
const width = fullConfig.theme?.width as {[key: string]: string};
const colors = fullConfig.theme?.colors as {[key:string]:string};
console.log(width['4]);
// => '1rem'
console.log(colors['red']);
/*
{
"50": "#fef2f2",
"100": "#fee2e2",
"200": "#fecaca",
"300": "#fca5a5",
"400": "#f87171",
"500": "#ef4444",
"600": "#dc2626",
"700": "#b91c1c",
"800": "#991b1b",
"900": "#7f1d1d"
}
*/ |
this was my solution /** @type {Parameters<Parameters<Parameters<import('tailwindcss/plugin')>[0]>[0]['addUtilities']>['0']} */ /** @type {NonNullable<NonNullable<import('tailwindcss').Config['theme']>['extend']>['colors']} */ /** @type {NonNullable<NonNullable<import('tailwindcss').Config['theme']>['extend']>['fontWeight']} */ /** @type {NonNullable<NonNullable<import('tailwindcss').Config['theme']>['extend']>['boxShadow']} */ /** @type {NonNullable<NonNullable<import('tailwindcss').Config['theme']>['extend']>['fontFamily']} */ |
I just need colors so far, so I ended up generating them before starting the dev server. I don't change my theme often, so this should be enough for now. I have this script for now: import resolveConfig from "tailwindcss/resolveConfig";
import tailwindConfig from "../tailwind.config.js";
import fs from "fs";
import * as prettier from "prettier";
async function main() {
const fullConfig = resolveConfig(tailwindConfig);
const contents = `/* Generated file, do not edit */
export const colors = ${JSON.stringify(fullConfig.theme?.colors)}`;
const filepath = "app/theme.ts";
const prettierOptions = (await prettier.resolveConfig(process.cwd())) ?? {};
prettierOptions.filepath = filepath;
const formatted = await prettier.format(contents, prettierOptions);
fs.writeFileSync(filepath, formatted);
}
main(); And then in "generate-theme": "tsx scripts/generate-theme.ts",
"dev": "npm run generate-theme && remix dev -c \"npm run dev:serve\"", Theme file looks pretty good now and TS is happy :) /* Generated file, do not edit */
export const colors = {
inherit: "inherit",
current: "currentColor",
transparent: "transparent",
black: "#000",
white: "#fff",
slate: {
"50": "#f8fafc",
"100": "#f1f5f9",
"200": "#e2e8f0",
"300": "#cbd5e1",
"400": "#94a3b8",
"500": "#64748b",
"600": "#475569",
"700": "#334155",
.... |
the simplest way that I found to get a typed config (so that I can get the autosuggestion for theme.colors): tailwind.config.ts
postcss.config.js
src/imports/theme.ts
|
For anyone coming to this thread for solutions in the future, now that the config file can be in typescript, using import { Config } from 'tailwindcss/types/config'
export default {
content: [/* ... */],
theme: {
// ...
},
} satisfies Config This accomplishes 2 things:
Note that without |
In my experience having the tailwind.config in the ts format and the satisfies was not enough, because I didn't have the autosuggestions for the default colors (it was detecting only the extended colors), It could work as an approach only if you override the entire default theme of tailwind and you write everything inside the config, but if you extend the theme and you still want to get the default configuration suggested it would not work as expected. |
@vimercati-samir Ah that is annoying! We mostly override the theme and do not need to reference anything from It sounds like the typing of |
Just doing this for now:
|
What version of Tailwind CSS are you using?
3.0.1
What build tool (or framework if it abstracts the build tool) are you using?
Next.js 12.0.7
What version of Node.js are you using?
For example: v14.18.1
What browser are you using?
Chrome
What operating system are you using?
macOS
Describe your issue
Documentation says to do this to reference tailwind abstraction from JS:
When I try to do this, I have the following errors in the latest version:
Seems something wrong with types for the latest release. I use tailwind types to describe my components props, so it's kind of critical to fix.
The text was updated successfully, but these errors were encountered: