Add TypeScript types for the tailwind.config.js file (#7891)

* add generate-types script

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.

* add types for the `tailwind.config.js` config file

* annotate stubs with a JSDoc pointing to the types

* add types to package.json

- 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.

* re-export types top-level

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`.

* also publish generated types

* update changelog

* enable TypeScript only when using `init --types` for now

* update tests to verify that `--types` works
This commit is contained in:
Robin Malfait 2022-03-22 10:24:28 +01:00 • committed by GitHub
parent 0578f7b5b5
commit 407a5c368c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 455 additions and 5 deletions

View file

@ -319,6 +319,46 @@ describe('Init command', () => {
// multiple keys in `theme` exists. However it loads `tailwindcss/colors`
// which doesn't exists in this context.
expect((await readOutputFile('../full.config.js')).split('\n').length).toBeGreaterThan(50)
expect(await readOutputFile('../full.config.js')).not.toContain(
`/** @type {import('tailwindcss/types').Config} */`
)
})
test('--types', async () => {
cleanupFile('simple.config.js')
let { combined } = await $(`${EXECUTABLE} init simple.config.js --types`)
expect(combined).toMatchInlineSnapshot(`
"
Created Tailwind CSS config file: simple.config.js
"
`)
expect(await readOutputFile('../simple.config.js')).toContain(
`/** @type {import('tailwindcss/types').Config} */`
)
})
test('--full --types', async () => {
cleanupFile('full.config.js')
let { combined } = await $(`${EXECUTABLE} init full.config.js --full --types`)
expect(combined).toMatchInlineSnapshot(`
"
Created Tailwind CSS config file: full.config.js
"
`)
// Not a clean way to test this. We could require the file and verify that
// multiple keys in `theme` exists. However it loads `tailwindcss/colors`
// which doesn't exists in this context.
expect((await readOutputFile('../full.config.js')).split('\n').length).toBeGreaterThan(50)
expect(await readOutputFile('../full.config.js')).toContain(
`/** @type {import('tailwindcss/types').Config} */`
)
})
test('--postcss', async () => {
@ -351,6 +391,7 @@ describe('Init command', () => {
Options:
-f, --full Initialize a full \`tailwind.config.js\` file
-p, --postcss Initialize a \`postcss.config.js\` file
--types Add TypeScript types for the \`tailwind.config.js\` file
-h, --help Display usage information
`)
)