Sub topic 13
Typing CSS Module imports in TypeScript
This chapter assumes you can write React components in TypeScript, import a .module.css file, and run a bundler such as Vite, webpack or Next.js. Without extra setup, TypeScript reports an error on every CSS Module import. Here you will see why that happens, how to fix it, and what the fix does not cover.
Why TypeScript complains
TypeScript only understands files it can parse: .ts, .tsx, .d.ts and, with options, .js and .json. A bundler turns import styles from './Button.module.css' into an object at build time. The compiler never sees that step. It looks for a module at that path, finds none, and reports an error like this:
Cannot find module './Button.module.css' or its corresponding type declarations.
The code still runs, because the bundler does not use TypeScript's check. The error only shows up in the editor and in tsc. The fix is to tell the compiler what such an import looks like.
Declaring the module shape
An ambient declaration describes modules that TypeScript cannot resolve on its own. This one matches every file that ends in .module.css.
declare module '*.module.css' {
const classes: { readonly [key: string]: string };
export default classes;
}The default export is an object. Each key is a class name, and each value is the generated, unique class name as a string. The readonly modifier stops code from assigning to the object by mistake.
Save the declaration in a file that ends in .d.ts, for example src/css-modules.d.ts. Then make sure tsconfig.json includes that file. If your include list covers src, the file is already picked up.
{
"compilerOptions": {
"jsx": "react-jsx",
"strict": true
},
"include": ["src"]
}Keep the file free of top-level import or export statements. A top-level import turns the file into a module, and the declare module then acts as an augmentation instead of a declaration of new modules. The wildcard would stop matching.
What you get from the declaration
With the declaration in place, the earlier component compiles as TSX.
import styles from './Button.module.css';
export function Button({ children }: { children: React.ReactNode }) {
return <button className={styles.button}>{children}</button>;
}The type is an index signature, so styles.button is typed as string. That brings two limits:
- A misspelled name such as
styles.butonpasses the type check. It evaluates toundefinedat runtime, so the element silently gets no class. - Editor autocomplete cannot suggest your class names.
If you set noUncheckedIndexedAccess in tsconfig.json, each lookup is typed string | undefined. That is more accurate, but you then need to handle undefined wherever you pass a class to className. Helpers such as clsx accept it without complaint.
Framework-provided types
Some tools ship the declaration for you, so you do not write your own:
| Tool | How to get the types |
|---|---|
| Vite | Add `/// <reference types="vite/client" />` to a `.d.ts` file in `src`. |
| Next.js | The generated `next-env.d.ts` references the types. Keep it in `include`. |
| webpack | Write the declaration above, or use a loader plugin that generates types. |
If your tool supplies the declaration, adding your own is harmless. Two matching declarations are merged, as long as both are written in the same form.
Getting exact class names
To catch typos, generate a .d.ts file for each stylesheet. Plugins such as typed-css-modules or typescript-plugin-css-modules read the CSS and produce a precise type that lists only the classes that exist. The trade-off is a build step or editor plugin to maintain, and generated files that must be kept in sync with the CSS. Many teams accept the loose index signature and rely on code review and tests instead.
Exercise
A teammate adds import styles from './Card.module.css' and gets "Cannot find module" in the editor, but the app runs fine in the browser. Why does this happen, and what is the fix?
Show answer
The bundler resolves the CSS import at build time, but TypeScript has no declaration for .module.css files. Add a .d.ts file in an included folder with declare module '*.module.css' that exports a default object of readonly string values, or reference the framework's types, such as vite/client. The file must not have top-level imports or exports.