Skip to content
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

Merged
merged 9 commits into from Mar 22, 2022
Merged

Conversation

RobinMalfait
Copy link
Contributor

@RobinMalfait RobinMalfait commented Mar 18, 2022

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 your tailwind.config.js file.

If you already have a tailwind.config.js file, then you should be able to convert it like this:

// Before
module.exports = {
  // ...
}

// After
/** @type {import('tailwindcss/types').Config} */
const config = {
  // ...
}

module.exports = config

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:

image

image

image

image

image

image

@RobinMalfait RobinMalfait force-pushed the add-types branch 3 times, most recently from 53ebc5e to c2a0d30 Compare March 18, 2022 23:45
@sasial-dev
Copy link

Will this also include support for ESM/imports?

@adamwathan
Copy link
Member

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 tailwind.config.ts files or anything. We’re exploring that as well but it’s proving a bit painful because the wider ecosystem doesn’t support that stuff that well. For example you can’t use an ESM PostCSS config anywhere we’ve tested, which means you still need some common JS config files even if we support ESM for Tailwind. Next.js doesn’t let you use a postcss.config.js file at all currently if your project is type: “module”, and the next.config.js file doesn’t support TypeScript even though Next otherwise does support TypeScript. So even if we added support for that stuff you’d still have a bunch of problems with tons of other tools you need to use alongside Tailwind, which makes it harder to see as a really big win. Exploring it though 👍🏻

@@ -1,4 +1,5 @@
module.exports = {
/** @type {import('tailwindcss/types')} */
const config = {
Copy link
Contributor

@innocenzi innocenzi Mar 19, 2022

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.

Copy link
Contributor Author

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 👍

@RobinMalfait RobinMalfait force-pushed the add-types branch 2 times, most recently from 05d10d9 to 187ce12 Compare March 21, 2022 16:19
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`.
@innocenzi
Copy link
Contributor

This just adds type declarations so people get autocompletions in the config file in their editor, not support for tailwind.config.ts files or anything. We’re exploring that as well but it’s proving a bit painful because the wider ecosystem doesn’t support that stuff that well. For example you can’t use an ESM PostCSS config anywhere we’ve tested, which means you still need some common JS config files even if we support ESM for Tailwind. Next.js doesn’t let you use a postcss.config.js file at all currently if your project is type: “module”, and the next.config.js file doesn’t support TypeScript even though Next otherwise does support TypeScript. So even if we added support for that stuff you’d still have a bunch of problems with tons of other tools you need to use alongside Tailwind, which makes it harder to see as a really big win. Exploring it though 👍🏻

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants