Now in the year 2021, all you need to do is add a src/Globals.d.ts to your project with these lines:

declare module "*.module.css";
declare module "*.module.scss";
// and so on for whatever flavor of css you're using

Then install and add

{
  "compilerOptions": {
    "plugins": [{ "name": "typescript-plugin-css-modules" }]
  }
}

to your tsconfig.

Example of this correctly functioning in VS code after making that simple change (root is a class defined in my stylesheet):

Webpack and tsc also compile correctly on the command line.

Answer from Matt Wonlaw on Stack Overflow
🌐
npm
npmjs.com › package › typescript-plugin-css-modules
typescript-plugin-css-modules - npm
July 23, 2025 - A TypeScript language service plugin for CSS Modules.
      » npm install typescript-plugin-css-modules
    
Published   Jul 23, 2025
Version   5.2.0
Author   Brody McKee
🌐
GitHub
github.com › mrmckeb › typescript-plugin-css-modules › releases
Releases · mrmckeb/typescript-plugin-css-modules
July 23, 2025 - A TypeScript language service plugin providing support for CSS Modules. - mrmckeb/typescript-plugin-css-modules
Author   mrmckeb
Discussions

reactjs - How to import CSS modules with Typescript, React and Webpack - Stack Overflow
"style-loader" : MiniCssExtrac... loader: 'css-loader', modules: true, }, "sass-loader", ], } ... I use create-react-app, but for some reason other solutions didn't work, something was missing; I think it's because VS Code uses its own TypeScript vesrion. So here is all I did until it finally worked; if you use create-react-app & VS Code it should work for you as well: run npm i typescript-plugin-css-modules ... More on stackoverflow.com
🌐 stackoverflow.com
TIL there is a typescript plugin for css modules
There’s also typed-css-modules and typed-scss-modules that work pretty well in my experience More on reddit.com
🌐 r/Frontend
4
30
August 31, 2022
What i am doing wrong?
Info 27 [13:26:55.840] Failed to load module 'typescript-plugin-css-modules' from /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/node_modules: Error: Could not resolve JS module 'typescript-plugin-css-modules' starting at '/Applications/Visual Studio ... More on github.com
🌐 github.com
10
July 18, 2020
reactjs - How can I import CSS Modules in a TypeScript file? - Stack Overflow
I am working with a combination ... to use CSS modules. I have tried a few Webpack configurations, and none of them are causing Webpack to use the correct loader. I have tried a few other variants of loaders (as seen in the commented out sections), and none of them are working. I am using an approach similar to this answer to import the stylesheets (declaring a src/Globals.d.ts, setting the typescript-plugin-css-modules ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › mrmckeb › typescript-plugin-css-modules
GitHub - mrmckeb/typescript-plugin-css-modules: A TypeScript language service plugin providing support for CSS Modules. · GitHub
A TypeScript language service plugin for CSS Modules.
Starred by 1.4K users
Forked by 77 users
Languages   TypeScript 93.3% | SCSS 2.0% | CSS 1.2% | JavaScript 1.0% | Sass 1.0% | Stylus 0.7%
Top answer
1 of 12
111

Now in the year 2021, all you need to do is add a src/Globals.d.ts to your project with these lines:

declare module "*.module.css";
declare module "*.module.scss";
// and so on for whatever flavor of css you're using

Then install and add

{
  "compilerOptions": {
    "plugins": [{ "name": "typescript-plugin-css-modules" }]
  }
}

to your tsconfig.

Example of this correctly functioning in VS code after making that simple change (root is a class defined in my stylesheet):

Webpack and tsc also compile correctly on the command line.

2 of 12
62

A) As you are saying, there is one simplest (not best) option to use require:

const css = require('./component.css')
  • We need to have typings for require as it's not standard feature in typescript.
  • Simplest typing for this specific require may be:

    declare function require(name: string): string;
    
  • Webpack will then compile typescript and use modules properly - BUT without any IDE help and class names checks for build.

B) There is better solution to use standard import:

import * as css from './component.css'
  • enables full class names IntelliSense
  • requires types definition for each css file, otherwise tsc compiler will fail

For proper IntelliSense, Webpack needs to generate types definition for each css file:

  1. Use webpack typings-for-css-modules-loader

    webpackConfig.module.loaders: [
      { test: /\.css$/, loader: 'typings-for-css-modules?modules' }
      { test: /\.scss$/, loader: 'typings-for-css-modules?modules&sass' }
    ];
    
  2. Loader will generate *.css.d.ts files for each of css files in your codebase

  3. Typescript compiler will understand that css import will be module with properties (class names) of type string.

Mentioned typings-for-css-loader contains a bug and because of types file generation delay, it's best to declare global *.css type in case our *.css.d.ts file is not generated yet.

That little bug scenario:

  1. Create css file component.css
  2. Include it in component import * as css from './component.css'
  3. Run webpack
  4. Typescript compiler will try to compile code (ERROR)
  5. Loader will generate Css modules typings file (component.css.d.ts), but it's late for typescript compiler to find new typings file
  6. Run webpack again will fix build error.

Easy fix is to create global definition (eg. file called typings.d.ts in your source root) for importing CSS Modules:

declare module '*.css' {
  interface IClassNames {
    [className: string]: string
  }
  const classNames: IClassNames;
  export = classNames;
}

This definition will be used if there is no css file generated (eg. you have added new css file). Otherwise will be used generated specific (needs to be in same folder and named same as source file + .d.ts extension), eg. component.css.d.ts definition and IntelliSense will work perfectly.

Example of component.css.d.ts:

export const wrapper: string;
export const button: string;
export const link: string;

And if you don't want to see generated css typings you may setup filter in IDE to hide all files with extension .css.d.ts in your sources.

🌐
Reddit
reddit.com › r/frontend › til there is a typescript plugin for css modules
r/Frontend on Reddit: TIL there is a typescript plugin for css modules
August 31, 2022 -

I just found this typescript plugin for css modules to make using classNames inside of components more typesafe (also gives intellisense)! https://www.npmjs.com/package/typescript-plugin-css-modules

with the plugin:

image showing usage of css modules with a typescript warning for the wrong class name

without the plugin there is no warning when you rename your css class:

image showing usage of css modules with no warnings
🌐
Puruvj
puruvj.dev › blog › css-modules-typescript-intellisense
Get the most out of CSS Modules with TypeScript in 5 minutes | blog | puruvj.dev
npm i --save-dev typescript-plugin-css-modules # Or if you're a yarn person yarn add --save-dev typescript-plugin-css-modules
Find elsewhere
🌐
GitHub
github.com › FredKSchott › snowpack › discussions › 1444
CSS Modules + TypeScript support · FredKSchott/snowpack · Discussion #1444
I wonder if we could recommend this for now: https://github.com/mrmckeb/typescript-plugin-css-modules.
Author   FredKSchott
🌐
CodeSandbox
codesandbox.io › examples › package › typescript-plugin-css-modules
typescript-plugin-css-modules examples - CodeSandbox
Use this online typescript-plugin-css-modules playground to view and fork typescript-plugin-css-modules example apps and templates on CodeSandbox.
🌐
Gatsby
gatsbyjs.com › plugins › gatsby-transformer-typescript-css-modules
gatsby-transformer-typescript-css-modules | Gatsby
and typings-for-css-modules-loader will read the CSS file and generates a .d.ts file alongside your css.
🌐
CodeSandbox
codesandbox.io › s › typescript-plugin-css-modules-0u1px
typescript-plugin-css-modules - CodeSandbox
January 30, 2020 - typescript-plugin-css-modules by inomdzhon using react, react-dom, react-scripts, typescript-plugin-css-modules
Published   Aug 29, 2019
Author   inomdzhon
🌐
xjavascript
xjavascript.com › blog › typescriptplugincssmodules
TypeScript Plugin for CSS Modules: A Comprehensive Guide — xjavascript.com
With CSS Modules, the class names will be transformed into unique identifiers, such as styles1_container__abc123 and styles2_container__def456, ensuring that each class only applies to the intended component. TypeScript is a statically typed superset of JavaScript that adds type checking to your code.
🌐
DEV Community
dev.to › kgf › supercharging-css-modules-with-typescript-1gnl
Supercharging CSS modules with TypeScript - DEV Community
April 20, 2024 - I've added the plugin to afterPlugins hook, but it hasn't resolved the issue. ... You can try: npm i -D typed-scss-modules Then npx typed-scss-modules src --watch It will automatically add type declaration · For further actions, you may consider blocking this person and/or reporting abuse · Kenneth G. Franqueiro ... Somewhat-musically-inclined software engineer for 15+ years. Primarily front-end, more recently full-stack TypeScript.
🌐
GitHub
github.com › mrmckeb › typescript-plugin-css-modules › issues
Issues · mrmckeb/typescript-plugin-css-modules
A TypeScript language service plugin providing support for CSS Modules. - mrmckeb/typescript-plugin-css-modules
Author   mrmckeb
🌐
GitHub
github.com › mrmckeb › typescript-plugin-css-modules › issues › 94
What i am doing wrong? · Issue #94 · mrmckeb/typescript-plugin-css-modules
July 18, 2020 - { "compilerOptions": { "outDir": "./build", "allowJs": true, "jsx": "react", "module": "commonjs", "target": "es6", "esModuleInterop": true, "plugins": [ { "name": "typescript-plugin-css-modules", "options": { "customMatcher": "\\.scss$" } } ] }, "include": [ "./src/**/*", "module.d.ts" ] }
Author   krutoo
🌐
GitHub
github.com › activeguild › typescript-plugin-css-modules-vite
GitHub - activeguild/typescript-plugin-css-modules-vite: Read the definition of vite.config.ts and resolve the CSS Modules type. Supports sass.
{ "compilerOptions": { ... "plugins": [{"name": "ts-css-modules-vite-plugin", "root": "./"}] }, } Resolve the preprocessorOptions setting within the plugin. import path from "path"; import { defineConfig } from "vite"; export default defineConfig({ css: { preprocessorOptions: { scss: { additionalData: `@use "@/styles" as common;`, importer(...args) { if (args[0] !== "@/styles") { return; } return { file: `${path.resolve(__dirname, "./src/assets/styles")}`, }; }, }, }, }, }); The VScode typescript version needs to match the project.
Author   activeguild
Top answer
1 of 2
3

import * as styles from "module-path"; is semantically different to import styles from "module-path";. Basically don't assume these non-javascript made-up modules follow any advanced module rules and always default import them.

EDIT: This answer is sort of outdated, in that the default way as of version 7.0.0 of css-loader is actually module import, i.e. import * as styles from "module-path"; and in that case styles.default would refer to .default class declaration within the css module file, not the default export of that css-but-actually-js-module. If changing from module import to default import fixes your problem after 2024-04-04, that means you have a dependency problem.

2 of 2
0

npm install --save-dev style-loader css-loader css-modules-dts-loader

and

const isNamedExport = true;
// webpack.config.js
module.exports = {
  // ... other webpack config settings
   module: {
      rules: [
         {
         test: /\.module\.(css|postcss|pcss|scss|sass|less|styl|sss)$/i,
         use: [
               "style-loader",
               {
                  loader: "css-modules-dts-loader",
                  options: {
                     // Convert CSS class names to camelCase (default: false)
                     camelCase: true,
                     // Quote style: "single" or "double" (default: "double")
                     quote: "single",
                     // Indentation style: "tab" or "space" (default: "space")
                     indentStyle: "space",
                     // Number of spaces if indentStyle is "space" (default: 2)
                     indentSize: 2,
                     // Mode: "emit" to generate or "verify" to check the file (default: "emit")
                     mode: isProduction ? "verify" : "emit",
                     // Sort the exported class names alphabetically (default: false)
                     sort: true,
                     // Use named exports instead of interface (default: true)
                     namedExport: isNamedExport,
                     // Custom banner comment at the top of the file
                     banner: "// This file is automatically generated.\n// Please do not change this file!"
                  }
               },
               {
                  loader: "css-loader",
                  options: {
                     sourceMap: true,
                     modules: {
                        namedExport: isNamedExport,
                        exportLocalsConvention: "as-is",
                        localIdentName: "[name]__[local]___[hash:base64]"
                     },
                     importLoaders: 1
                  }
               }
            ]
         }
      ]
   }
};

and import styles by the next way

if isNamedExport is true

import * as styles from "$componentName.module.css"

if isNamedExport is false

import styles from "$componentName.module.css"