CSS Modules Support for TypeScript Language Service

5.2.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates installing the plugin, configuring `tsconfig.json`, and importing CSS Modules with both default and named exports in a React component, providing type safety for class names.

/* 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;
// }

view raw JSON →