CSS Modules Support for TypeScript Language Service
typescript-plugin-css-modules is a TypeScript language service plugin that enhances developer experience by providing type information and autocomplete for CSS Modules within IDEs. It allows developers to safely use CSS class names, including those from SCSS, Sass, Less, and Stylus files, by inferring types from their module exports. The current stable version is 5.2.0. This plugin primarily focuses on design-time support for tools that leverage TypeScript's language service, such as VS Code, rather than providing compilation-time error checking or direct CSS Module bundling/processing. Its release cadence is generally every few months for minor versions, with patch releases as needed. A key differentiator is its seamless integration with the TypeScript language service to provide strong typing and intellisense for CSS module exports, catching typos and providing auto-completion where standard TypeScript or bundler configurations might only offer generic `[key: string]: string` types.
Common errors
-
Cannot find module './my-component.module.css' or its corresponding type declarations.
cause TypeScript compiler cannot find a `.d.ts` declaration for CSS modules, or the plugin is not correctly configured/loaded.fix1. Ensure `typescript-plugin-css-modules` is listed in `compilerOptions.plugins` in `tsconfig.json`. 2. For broader compatibility and to avoid build errors, add global type declarations for CSS modules (e.g., `declare module '*.module.css';`) in a `.d.ts` file in your project. -
Property 'myClass' does not exist on type '{ readonly [key: string]: string; }'.cause The TypeScript language service plugin is not active, or a generic CSS module declaration is overriding the plugin's specific type inference, meaning the IDE isn't providing specific types for your CSS classes.fixVerify `tsconfig.json` plugin configuration. If using VS Code, ensure it's set to 'Use Workspace Version' of TypeScript. Check for conflicting global `.d.ts` declarations that might provide a generic `[key: string]: string` type without the specific class names. -
File '/project/src/App.module.scss' is not listed within the file list of project '/project/tsconfig.json'. Projects must list all files or use an 'include' pattern.
cause This error can occur with TypeScript 5 and `composite: true` when the plugin is active, suggesting an issue with how the plugin interacts with TypeScript's project file discovery for `.scss` modules.fixThis specific issue (related to TS5 and `composite: true`) points to a bug. Refer to the project's GitHub issues (`mrmckeb/typescript-plugin-css-modules#222`) for the latest status and potential workarounds, which may involve explicit `include` patterns or plugin options.
Warnings
- breaking Version 5.0.0 introduced a breaking change requiring TypeScript 4.x as the minimum supported version. Projects using older TypeScript versions must upgrade to at least 4.0.0.
- gotcha This plugin *only* provides type information to IDEs and does not provide errors during compilation nor adds actual CSS module support to your project build. For compilation, a separate build setup (e.g., Webpack's `css-loader`, Jest transformers) is required.
- gotcha For Visual Studio Code users, it is highly recommended to configure VS Code to use the workspace's version of TypeScript, not its bundled version. Failure to do so may result in the plugin not activating or providing incorrect type suggestions.
- gotcha Using Sass `@use` rules within SCSS modules might lead to `typescript-plugin-css-modules` reporting an empty interface `{}` for the imported module, causing errors on class names in the IDE. This is a known issue.
- deprecated The `Stylus` renderer became an optional dependency in v5.2.0. If you use Stylus files (`.styl`), you need to explicitly install `stylus` as a dev dependency in your project.
Install
-
npm install typescript-plugin-css-modules -
yarn add typescript-plugin-css-modules -
pnpm add typescript-plugin-css-modules
Imports
- styles
const styles = require('./my-component.module.css');import styles from './my-component.module.css';
- myClass
import { my_class } from './my-component.module.css';import { myClass } from './my-component.module.css'; - Global CSS Module Declaration
import * as styles from './my-component.module.css';
declare module '*.module.css' { const classes: { [key: string]: string }; export default classes; }
Quickstart
/* src/App.module.css */
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.title {
color: #333;
font-size: 2em;
}
.highlightText {
color: #007bff;
font-weight: bold;
}
/* src/App.tsx */
import React from 'react';
import styles, { title, highlightText } from './App.module.css';
interface AppProps {
message: string;
}
const App: React.FC<AppProps> = ({ message }) => {
return (
<div className={styles.container}>
<h1 className={title}>
Hello, <span className={highlightText}>{message}</span>!
</h1>
</div>
);
};
export default App;
// tsconfig.json
// {
// "compilerOptions": {
// "plugins": [{ "name": "typescript-plugin-css-modules" }],
// "jsx": "react-jsx"
// }
// }
// src/global.d.ts (optional, but recommended for full type safety)
// declare module '*.module.css' {
// const classes: { readonly [key: string]: string };
// export default classes;
// }