Skip to content

Nested rules not correctly typed with PostCSS plugin #197

Closed
@daanvosdewael

Description

@daanvosdewael

Describe the bug
When using the postcss-nested plugin to nest rules, the types are not generated as expected.

To Reproduce
Steps to reproduce the behavior:

  1. Add a PostCSS config:
// postcss.config.js
module.exports = {
  plugins: {
    'postcss-nested': {},
  },
};
  1. Configure the plugin to have the useConfig boolean on the postCssOptions object set to true:
// tsconfig.json
{
  "name": "typescript-plugin-css-modules",
  "options": {
    "postCssOptions": {
      "useConfig": true
    }
  }
}
  1. Make a CSS module file with nested rules:
/* Foo.module.css */
.foo {
  width: 100%;

  &__bar {
    z-index: 1;
  }
}
  1. Use the styles in JS:
import styles from './Foo.module.css';
console.log(styles['foo__bar']); // Property 'foo__bar' does not exist on type '{ foo: string; }'.

Expected behavior
Because of the & in the selector, this nested rule becomes a new selector in addition to the root one.

The type definition of styles should be:

{
  foo: string;
  'foo__bar': string;
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions