corvid
corvid

Reputation: 11187

TypeScript not finding style modules

I have the following basic configuration in my webpack which is relevant to loading my styles via TypeScript:

{
  module: {
    rules: [{
      test: /\.s?css$/,
      use: [
        { loader: 'style-loader' },
        {
          loader: 'typings-for-css-modules-loader',
          options: {
            sass: true,
            modules: true,
            camelCase: true,
            importLoaders: 1,
            namedExport: true,
            localIdentName: '[name]__[local]__[hash:base64:5]',
          }
        },
        { loader: 'sass-loader' },
      ]
    }],
  },
  resolve: {
    alias: {
      app: srcPath,
    },
    modules: ['node_modules', srcPath],
    extensions: ['.js', '.jsx', '.json', '.scss', '.css', '.tsx', '.ts', '.d.ts'],
  },
}

In my tsconfig.json, I have the following to match:

{
  "compilerOptions": {
    "paths": {
      "app": ["./app"]
    }
  }
}

However, I am consistently getting an error when trying to load a file from app/styles/root.scss, with the following project structure:

├── components
│   └── Root.tsx
├── styles
│   └── root.scss
├── index.hbs
├── index.scss
├── index.scss.d.ts
└── index.tsx

enter image description here

It seems like TypeScript is not able to resolve this path correctly. If this were a plain JS based file, it would work fine.

Strangely, I can add a root.scss.d.ts with the defintions for the CSS file and it will resolve correctly. I think this is a bit strange because I am specifically using typings-for-css-modules-loader for this task.

How do I get TypeScript to resolve these scss files under the styles directory?

Upvotes: 1

Views: 1071

Answers (1)

basarat
basarat

Reputation: 275947

Fix

Simply

declare module "*.scss";

More

See the JavaScript to TypeScript migration guide : https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html

Upvotes: 1

Related Questions