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
Add TypeScript types for the tailwind.config.js
file
#7891
Conversation
53ebc5e
to
c2a0d30
Compare
Will this also include support for ESM/imports? |
This just adds type declarations so people get autocompletions in the config file in their editor, not support for |
stubs/defaultConfig.stub.js
Outdated
@@ -1,4 +1,5 @@ | |||
module.exports = { | |||
/** @type {import('tailwindcss/types')} */ | |||
const config = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding a defineConfig
function export?
// index.js
module.exports.defineConfig = (config) => config
// types.d.ts
export function defineConfig(config: Config): Config
So we can have this tailwind.config.js
:
// @ts-check
const { defineConfig } = require('tailwindcss')
module.exports = defineConfig({
content: [],
presets: [],
darkMode: 'media', // or 'class'
plugins: [],
})
I think this is a really nice pattern for typed configuration objects, to avoid the JSDOC syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do that in the future if we need to, but for now I don't see a lot of benefits. We still have to import something and still have to annotate something. Thanks for the suggestion though 👍
05d10d9
to
187ce12
Compare
This script will generate the full list of core plugins, which will allow you to get code completion for the `corePlugins` section. It will also generate all the colors (and deprecated colors) which is used in multiple places in the config.
- Updated the files to make sure that the types are being published - Add a `types` section in the `package.json`, otherwise your editor by default will look for the `DefinitelyTyped` types which got me really confused for a second. - Added some scripts to make sure that the generation of types happens when needed (before tests and before building). This way you never ever have to think about generating them when working on Tailwind CSS internals.
Having a `colors.d.ts` next to the `colors.js` file allows us to type the `colors.js` file and your editor will pickup the types from `colors.d.ts`.
Hey @adamwathan, have you explored the "transpile config file" route? I think it's way easier to add ESM/TS support this way right now, precisely for the reasons you mentioned. If Tailwind takes care of that config file transpilation, other tooling don't have anything to do about it right? |
This PR will add types for the
tailwind.config.js
file. It also adds types for common tools we expose like:require('tailwindcss/colors')
require('tailwindcss/defaultConfig')
require('tailwindcss/defaultTheme')
require('tailwindcss/plugin')
Currently, this is an opt-in feature. When you generate a new tailwind config you can add a
--types
flag to also add types to yourtailwind.config.js
file.If you already have a
tailwind.config.js
file, then you should be able to convert it like this:Note: we separated the config & the module.exports line, this is because in some editors (even vscode) you will get
module
related code completion if you just did/** @type {...} */module.exports = ...
.Here are a few screenshots of what those types get you: