Skip to content

Instantly share code, notes, and snippets.

@wiledal
Last active January 4, 2022 16:04
Show Gist options
  • Save wiledal/13ecb4feb2f2761ac39e9e5fa7f2a835 to your computer and use it in GitHub Desktop.
Save wiledal/13ecb4feb2f2761ac39e9e5fa7f2a835 to your computer and use it in GitHub Desktop.
Add typed css modules in nextjs

Add autocompleted types to imported css modules in NextJS (or another React project)

Original article: https://apoorv.blog/typed-css-modules-reactjs/

  1. Add the typescript-plugin:
yarn add --dev typescript-plugin-css-modules
  1. Add the plugin to tsconfig:

tsconfig.json

{
  ...
  "plugins": [{
    "name": "typescript-plugin-css-modules"
  }]
}
  1. Add declaration in /types

types/css.d.ts

declare module "*.module.css" {
  const classes: { [key: string]: string };
  export default classes;
}

declare module "*.module.scss" {
  const classes: { [key: string]: string };
  export default classes;
}
  1. Add vscode settings file, to use local TypeScript

.vscode/settings.json

{
  "typescript.tsdk": "node_modules/typescript/lib",
  "typescript.enablePromptUseWorkspaceTsdk": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment