Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript declare specific SCSS module

I'm trying to declare a specific SCSS module to import from TypeScript. I have this declaration file that is working well:

declare module '*.scss' {
  const styles: {
    a: string
    b: string
    c: string
  }

  export default styles
}

But I'd like to narrow down *.scss somehow so that this declaration file only applies to the SCSS file _variables.scss which is located in the same directory.

I have tried to replace *.scss with _variables.scss or ./_variables.scss but then TypeScript wasn't able to find the module declaration at all.

Is this possible with TypeScript?

like image 614
heroxav Avatar asked Oct 04 '20 18:10

heroxav


People also ask

Is there a way to integrate CSS modules with typescript?

There is a spectrum for how CSS Modules can be integrated with TypeScript. Depending on the specific use case, existing codebase, and other technologies being used there are various tools that can be used that fit the different needs.

How to use CSS module in typescript + Webpack + Sass projects?

When using CSS Module in Typescript + Webpack + Sass projects, you can use CSS modules normally, but vscode always prompts cannot find module'XXX.scss' or its corresponding type declarations. First of all, make sure that webpack and sass have been able to recognize the CSS Module, please refer to the webpack official website configuration.

How do I export a module in typescript?

TypeScript supports export =to model the traditional CommonJS and AMD workflow. The export =syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum. When exporting a module using export =, TypeScript-specific import module = require("module")must be used to import the module.

What is typed-SCSS-modules and how to use it?

As the name suggests, typed-scss-modules is heavily inspired by typed-css-modules. It’s a CLI that generates type definitions focused on supporting CSS Modules written using SASS. Example of writing a SASS CSS Module while generating the corresponding type definitions.


1 Answers

You can do your module declaration like this. Read more on wildcard module declarations here.

declare module '*_variables.scss' {
  const styles: {
    a: string
    b: string
    c: string
  }

  export default styles
}
like image 136
JoshA Avatar answered Sep 29 '22 09:09

JoshA