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

🌐
npm
npmjs.com › package › typescript-plugin-css-modules
typescript-plugin-css-modules - npm
July 23, 2025 - Start using typescript-plugin-css-modules in your project by running `npm i typescript-plugin-css-modules`. There are 118 other projects in the npm registry using typescript-plugin-css-modules.
      » npm install typescript-plugin-css-modules
    
Published   Jul 23, 2025
Version   5.2.0
Author   Brody McKee
Discussions

What's the best way to get typescript support for css module imports in next?
Same here, I didn't find a perfect solution. typescript-plugin-css-modules is great, but has some limitations. More on reddit.com
🌐 r/nextjs
5
3
March 29, 2022
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
Typescript + CSS Modules + Rollup
Some css loader plugins automatically convert classes to camelCase before hashing. Which means your "Tag" would actually need to be "tag" to be recognized. On my phone so not diggin too deep but would try making them both lower case to start. More on reddit.com
🌐 r/typescript
3
2
June 30, 2022
Using CSS Modules with CRA+Typescript

Consider using something like emotion or styled components instead.

More on reddit.com
🌐 r/reactjs
3
3
October 23, 2018
🌐
Medium
medium.com › @dimi_2011 › setting-up-css-modules-in-typescript-project-52596526d19
Setting up CSS Modules in Typescript project | by Ivan Dimitrijevic | Medium
March 21, 2023 - To replace blueBox and redLabel with some hash that will isolate our module and allow us to run the code above the few steps are needed. Step 1 We need to define typings.d.ts file in root of our project in which we will describe how css module should be threated. ... Step 2 We need to install typescript-plugin-css-modules with npm.
🌐
DEV Community
dev.to › kgf › supercharging-css-modules-with-typescript-1gnl
Supercharging CSS modules with TypeScript - DEV Community
April 20, 2024 - In this post, I aimed to demonstrate how with the right tools, CSS modules and TypeScript can be used as a basis for configurable core UI components, with no runtime CSS-in-JS required.
🌐
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 providing support for CSS Modules. - mrmckeb/typescript-plugin-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%
🌐
LogRocket
blog.logrocket.com › home › how to write type-safe css modules
How to write type-safe CSS Modules - LogRocket Blog
June 4, 2024 - For example, we could build a CSS to TypeScript definition extractor. However, to avoid re-inventing the wheel, we’ll leverage the open source package typed-css-modules. Let’s get to it! Install the package in your project with npm i typed-css-modules, then add the type-generation to your main development script in the package.json scripts:
🌐
GitHub
github.com › Quramy › typed-css-modules
GitHub - Quramy/typed-css-modules: Creates .d.ts files from CSS Modules .css files · GitHub
Creates TypeScript definition files from CSS Modules .css files.
Starred by 1.1K users
Forked by 68 users
Languages   TypeScript 80.4% | JavaScript 16.8% | CSS 2.3%
Find elsewhere
🌐
Puruvj
puruvj.dev › blog › css-modules-typescript-intellisense
Get the most out of CSS Modules with TypeScript in 5 minutes | blog | puruvj.dev
And the best part: Because it’s just an npm package, and because the tsconfig is there, anyone else(team member/collaborator) will have the same experience as you out of the box, without any config. No need for them to download any extension, just the plain old npm install will do it for them. ... Install this little npm package, preferably as a devDependency (-D). npm i --save-dev typescript-plugin-css-modules # Or if you're a yarn person yarn add --save-dev typescript-plugin-css-modules
🌐
egghead.io
egghead.io › lessons › webpack-import-css-modules-with-typescript-and-webpack
Import CSS Modules with TypeScript and webpack | egghead.io
If you try to use CSS Modules in ... './styles.css'”. typings-for-css-modules-loader is a drop-in replacement for css-loader that works with TypeScript and generates typings for CSS on the fly....
Published   January 25, 2018
🌐
Atomic Spin
spin.atomicobject.com › css-module-typescript
Increasing the Value of CSS Modules with Typescript
October 24, 2023 - Since the project already uses Typescript, we can utilize it to help declare the types of our styles object. You could do this yourself by creating a declaration file for each of our *.module.css files, but that’s a lot of extra work just to check for spelling errors.
🌐
Quramy
quramy.github.io › typescript-css-modules-demo
CSS Modules TypeScript Demo
We cannot provide a description for this page right now
🌐
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. This would get you the IDE feedback AND supports Less/Sass/Stylus without any added complexity on our end.
Author   FredKSchott
🌐
Medium
medium.com › @sapegin › css-modules-with-typescript-and-webpack-6b221ebe5f10
How to use CSS Modules with TypeScript and webpack | by Artem Sapegin | Medium
March 27, 2019 - You can bypass TypeScript import magic by using require instead of import: ... It’s processed by webpack as usual but you won’t have type check and autocomplete for CSS class names. To use import you need typings for CSS. For example, you have Button.css like this: .foo { color: chocolate; } .barBaz { color: tomato; } ... typings-for-css-modules-loader is a drop-in replacement for css-loader that generates typings for CSS on the fly.
🌐
npm
npmjs.com › package › @types › css-modules
types/css-modules - TypeScript definitions
November 7, 2023 - interface CSSModule { /** * Returns the specific selector from imported stylesheet as string. */ [key: string]: string; } declare module "*.css" { /** * A CSS module. */ const styles: CSSModule; export default styles; } declare module "*.scss" ...
      » npm install @types/css-modules
    
🌐
Reddit
reddit.com › r/nextjs › what's the best way to get typescript support for css module imports in next?
r/nextjs on Reddit: What's the best way to get typescript support for css module imports in next?
March 29, 2022 -

We use next.js with typescript and css modules, but when we import style objects from a css module that style object just has a type of:

{
  readonly [key: string]: string;
}

We'd like to improve on that so we can actually see what classes are supported through the style object and also get additional type safety by having the compiler check if a class exists.

We found a few projects that can do this, one is a typescript plugin that doesn't support compile time errors, but does support IDE hints, and the other one is a webpack plugin that generates typescript declarations for the module files.

I'm curious if anyone has experience adding this kind of functionality on top of next js and what approach works best.

🌐
npm
npmjs.com › package › typed-css-modules
typed-css-modules - npm
January 31, 2024 - Creates TypeScript definition files from CSS Modules .css files.
      » npm install typed-css-modules
    
Published   Jan 31, 2024
Version   0.9.1
Author   quramy
🌐
Skovy
skovy.dev › blog › generating-typescript-definitions-for-css-modules-using-sass
Generating TypeScript definitions for CSS Modules using SASS | Spencer Miskoviak
November 7, 2020 - Tooling and approaches for integrating CSS Modules, SASS, and TypeScript to add additional type-safety when importing the styles.
🌐
xjavascript
xjavascript.com › blog › typescriptplugincssmodules
TypeScript Plugin for CSS Modules: A Comprehensive Guide — xjavascript.com
With CSS Modules, the class names ... class only applies to the intended component. TypeScript is a statically typed superset of JavaScript that adds type checking to your code....
🌐
Medium
medium.com › @shrestha.sudaman › using-css-modules-with-typescript-a-puzzle-01d62420eb49
Using CSS Modules with TypeScript | by Sudaman Shrestha | Medium
November 14, 2025 - When it comes to NPM packages, typescript-plugin-css-modules is a popular choice. It works fine with VS Code but requires setting up VS Code to use the workspace version of TypeScript.
🌐
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