Replies: 26 comments 21 replies
-
Would be open to a PR for this, not something I have experience with myself though. One thing to be careful of though is that you can have any keys you want in that file, because plugins can expect custom keys to exist, so I'm not sure how easy it's going to be to capture that flexibility in a type definition. |
Beta Was this translation helpful? Give feedback.
-
I was about to open the same issue. Glad there are more people interested in this. @adamwathan in typescript you can define something like: interface Dictionary {
some: string;
keys: string;
/**
* and with this you are saying: "this type can have any field
* as long as the key is a string and it also return a string"
*/
[key: string]: string;
} @Glinkis I don't know how to "wire" the type definition to the raw javascript file, but I can help write the definition file if wanted |
Beta Was this translation helpful? Give feedback.
-
Not quite sure what you mean by this. It should be enough to just create a One can then simply do Would you like to create a branch for this, and link it here? That way we could potentially collaborate on the type file. |
Beta Was this translation helpful? Give feedback.
-
@Glinkis I will create it on the weekend, I already have some types from a experiment that I did on tailwind. The only thing I can't get right are the default values. Just inlining all the default values on a e.g If you know something that could help (like a tool that extracts default values from an existing object) that would solve this problem. |
Beta Was this translation helpful? Give feedback.
-
@LouizFC I started a bit over here, with a very basic setup. |
Beta Was this translation helpful? Give feedback.
-
FYI, this gets you fairly close: /** @typedef { import('tailwindcss/defaultConfig') } DefaultConfig */
/** @typedef { import('tailwindcss/defaultTheme') } DefaultTheme */
/** @type { DefaultConfig & { theme: { extend: DefaultTheme } } } */
module.exports = {
// you have autocomplete here, type "c" and it'll suggest "corePlugins"
} It's not perfect (e.g. |
Beta Was this translation helpful? Give feedback.
-
@jonaskuske thanks seems great |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
@jonaskuske thanks, exactly what I'm looking for as you said I created a tailwind.config.js file with : /** @typedef { import('tailwindcss/defaultConfig') } DefaultConfig */
/** @typedef { import('tailwindcss/defaultTheme') } DefaultTheme */
/** @typedef { DefaultConfig & { theme: { extend: DefaultTheme } } } TailwindConfig */
/** @type {TailwindConfig} */
module.exports = {
// corePlugins hints working here
} then in my TS file: import { TailwindConfig } from './tailwind.config'
const config: TailwindConfig = { test: 1 }
//don't have any errors or autocompletion hints here Do you know what is wrong with my code ? |
Beta Was this translation helpful? Give feedback.
-
@GaspardC Hmm, good question. I tested it in Alright, my mistake: |
Beta Was this translation helpful? Give feedback.
-
With |
Beta Was this translation helpful? Give feedback.
-
I have created high quality type definitions for tailwind: DefinitelyTyped/DefinitelyTyped#50921 It will be downloadable by doing Enjoy! |
Beta Was this translation helpful? Give feedback.
-
Tailwindcss types are now published! |
Beta Was this translation helpful? Give feedback.
-
For posterity:
Enjoy types and autocompletion! |
Beta Was this translation helpful? Give feedback.
-
I tried @idan 's suggestion in my Vue3 + TS project but it didn't work. After trying a few other things, what finally did the trick was creating a tailwind.config.d.ts file with:
Then in the .vue file where i dynamically generate a color palette component for Storybook:
Hope this helps someone else facing the same issue. |
Beta Was this translation helpful? Give feedback.
-
Having real trouble with this in vue 3 using vite. I've tried @idan's suggestion and @jjdive's but neither worked. Has anyone managed to do this with vue 3 and vite? |
Beta Was this translation helpful? Give feedback.
-
I wrote my own types for myself. https://github.com/lightyen/tailwind-types |
Beta Was this translation helpful? Give feedback.
-
Honestly, the type definitions used here are far better than those present on DefinitelyTyped ( |
Beta Was this translation helpful? Give feedback.
-
npm i -D @types/tailwindcss tailwind.config.js /** @type {import('tailwindcss/tailwind-config').TailwindConfig} */
module.exports = {
// ...
} |
Beta Was this translation helpful? Give feedback.
-
Am I the only one having trouble with this type? |
Beta Was this translation helpful? Give feedback.
-
We've improved I did make an attempt at adding a I decided to call it quits because of that difficulty. If there's ever any interest in converting Tailwind to TypeScript and adding |
Beta Was this translation helpful? Give feedback.
-
@adamwathan do you have any plans to integrate TS types into tailwind as a 1st class? DefinitelyTyped is great, but 1st class support is much better. |
Beta Was this translation helpful? Give feedback.
-
Is there an issue (feature request) or PR on this topic? I can't find any, maybe we should open one? I would love to have type definitions maintained by the Tailwind team and being able to use the |
Beta Was this translation helpful? Give feedback.
-
does anyone know how to get this working with
But I get Even though I have
|
Beta Was this translation helpful? Give feedback.
-
So for anyone pulling their hair out over fully inferred custom tailwind configs, try using typescripts Root config within the workspace that all other tailwind configs inherit // tailwind-workspace-config.ts
import type { Config } from "tailwindcss/types/config";
const config = {
content: [],
theme: {
extend: {
colors: {
foo: "#ffffff",
},
},
},
} satisfies Config;
export default config; Exporting the theme within a library called 'ui' in the libs/web/common folder. // libs/web/common/ui/src/lib/theme.ts
import resolveConfig from "tailwindcss/resolveConfig";
// eslint-disable-next-line @nx/enforce-module-boundaries
import config from "../../../../../../../tailwind-workspace-preset";
export const theme = resolveConfig(config).theme;
export const fooColor = theme.colors.foo; // <-- typescript is inferring my custom color and all default colors Now go back to your tailwind config (root config?) and declare the type as |
Beta Was this translation helpful? Give feedback.
-
For types in my /** @type {import('tailwindcss/defaultConfig')} */ |
Beta Was this translation helpful? Give feedback.
-
It would be awesome if we were able to get autocomplete working in the configuration file. Especially so with
theme
callbacks and plugins.This should be possible to achieve by including typescript or jsdoc type definitions, and then doing something like:
Beta Was this translation helpful? Give feedback.
All reactions