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

🌐
Atomic Spin
spin.atomicobject.com › css-module-typescript
Increasing the Value of CSS Modules with Typescript
October 24, 2023 - create-react-app project-name --template typescript · CSS Modules are typically indicated by the filename *.module.css, but can also be *.module.less or *.module.scss, depending on the CSS preprocessor you’re using.
🌐
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 › FredKSchott › snowpack › discussions › 1444
CSS Modules + TypeScript support · FredKSchott/snowpack · Discussion #1444
As users of CSS Modules and TypeScript ourselves, I’d love to add support for it. Currently if you tried to run: import styles from './styles.module.css' TypeScript would throw an error bec...
Author   FredKSchott
🌐
Quramy
quramy.github.io › typescript-css-modules-demo
CSS Modules TypeScript Demo
We cannot provide a description for this page right now
Find elsewhere
🌐
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%
🌐
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" { /** * An SCSS based CSS module.
      » npm install @types/css-modules
    
🌐
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
🌐
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%
🌐
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
🌐
LogRocket
blog.logrocket.com › home › how to write type-safe css modules
How to write type-safe CSS Modules - LogRocket Blog
June 4, 2024 - We’ll generate the types automatically instead of manually, and we’ll provide a script to verify that the generated types are up-to-date to avoid incorrect CSS Module typings leaking into the compilation step. There are multiple ways to achieve this. For example, we could build a CSS to TypeScript definition extractor.
🌐
CodeSandbox
codesandbox.io › s › react-typescript-css-modules-se000
React + TypeScript + CSS Modules - CodeSandbox
August 12, 2023 - React + TypeScript + CSS Modules by izuchy using @types/classnames, classnames, react, react-dom, react-scripts
Published   Feb 02, 2021
Author   izuchy
🌐
npm
npmjs.com › package › css-modules-typescript-loader
css-modules-typescript-loader - npm
September 22, 2020 - Webpack loader to create TypeScript declarations for CSS Modules. Latest version: 4.0.1, last published: 5 years ago. Start using css-modules-typescript-loader in your project by running `npm i css-modules-typescript-loader`. There are 55 other projects in the npm registry using css-modules-typescript-loader.
      » npm install css-modules-typescript-loader
    
Published   Sep 22, 2020
Version   4.0.1
Author   SEEK
🌐
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.
🌐
Medium
revs.runtime-revolution.com › setting-up-a-react-components-library-in-typescript-with-css-modules-882a10c1d9e6
Setting up a React components library in TypeScript with CSS Modules | by Marcos Germano | Runtime Revolution
April 17, 2023 - With this configuration, our final distribution CSS will be exported in a single file, containing all classes used throughout our library. Each class name is slightly changed in this process, to include a prefix and a hash, so as to make sure we never run into conflicts when importing our library in other places. NOTE: It’s very important that we set autoModules and onlyModules as false, otherwise the modules will neither be exported nor used correctly in our components.
🌐
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