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?
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.
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With