Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import scss file as variable in react with typescript

Tags:

reactjs

I am unable to import scss file in below format.

import * as styles from './index.scss';

getting below error :

can not find module './index.scss'

I want to use class name like below :

className={styles.anyClassName}
like image 882
Arif Khan Avatar asked Jun 26 '18 08:06

Arif Khan


People also ask

Does react work with SCSS?

Introduction: We can use SASS in React using a package called node-sass. Using node-sass we can just create sass files and use them as the normal CSS files in our React application and node-sass will take care of compiling sass files.


1 Answers

In order to import SCSS as object in TS like this :

import * as styles from './index.scss';
  • Create file 'global.d.ts' in your source directory (or outside, but must be included in the src directory defined in tsconfig)
  • Add the following code inside
declare module '*.scss' {
  const content: {[className: string]: string};
  export = content;
}

https://basarat.gitbooks.io/typescript/docs/types/ambient/d.ts.html

like image 53
user2668735 Avatar answered Sep 18 '22 19:09

user2668735