How do I prevent webpack from throwing typescript errors for unused modules?

I have the following structure:

└── src
    ├── tsconfig.json
    ├── core
    │   ├── [...].ts
    └── ui
        ├── [...].tsx
        └── tsconfig.json

In the frontend I import a small number of modules from the core. These modules, and any dependent modules, are compliant with both tsconfig files.

tsc and eslint pass with no errors and webpack builds the desired output file. So far so good.

However when webpack builds it throws loads of type errors for other backend modules.

How do I suppress these errors? I tried excluding src/core from babel-loader and including the required modules but I was still getting the same errors.

/// webpack.config.js
/* eslint-disable @typescript-eslint/no-var-requires */

const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'development',

  entry: './src/ui',

  output: {
    path: path.resolve(__dirname, './ui-dist'),
  },

  // Enable sourcemaps for debugging webpack's output.
  devtool: 'source-map',

  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: ['.ts', '.tsx', '.js', '.jsx'],
  },

  module: {
    rules: [
      {
        test: /\.(j|t)sx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            cacheDirectory: true,
            babelrc: false,
            presets: [
              [
                '@babel/preset-env',
                { targets: { browsers: 'last 2 versions' } }, // or whatever your project requires
              ],
              '@babel/preset-typescript',
              '@babel/preset-react',
            ],
            plugins: [
              // plugin-proposal-decorators is only needed if you're using experimental decorators
              //  in TypeScript
              ['@babel/plugin-proposal-decorators', { legacy: true }],
              ['@babel/plugin-transform-runtime', { legacy: true }],
              ['@babel/plugin-proposal-class-properties', { loose: true }],
              'react-hot-loader/babel',
            ],
          },
        },
      },
      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      {
        enforce: 'pre',
        test: /\.js$/,
        loader: 'source-map-loader',
      },
    ],
  },
  plugins: [
    new ForkTsCheckerWebpackPlugin({
      tsconfig: path.resolve(__dirname, './src/ui/tsconfig.json'),
      eslint: true,
      /** Options to supply to eslint https://eslint.org/docs/developer-guide/nodejs-api#cliengine */
      // eslintOptions: EslintOptions;
    }),
    new HtmlWebpackPlugin({
      title: 'development',
      template: './src/ui/template.html',
    }),
  ],
  // When importing a module whose path matches one of the following, just
  // assume a corresponding global variable exists and use that instead.
  // This is important because it allows us to avoid bundling all of our
  // dependencies, which allows browsers to cache those libraries between builds.
  // externals: {
  //   react: 'React',
  //   'react-dom': 'ReactDOM',
  // },
  devServer: {
    contentBase: path.resolve(__dirname, './ui-dist'),
  },
};

EDIT: I suppose I am referencing these modules throwing an error by using import type { x } from '../core/index.ts'. Perhaps I need to find a way for babel-loader to skip scanning type imports.

115 thoughts on “How do I prevent webpack from throwing typescript errors for unused modules?”

  1. Simply wish to say your article is as astounding.
    The clarity for your post is just great and that i
    could think you’re a professional in this subject.
    Fine along with your permission let me to grasp your feed
    to stay updated with coming near near post. Thanks 1,000,000 and please keep up the gratifying work.

    Reply
  2. Your style is so unique compared to other people I’ve read stuff from.

    Thank you for posting when you’ve got the opportunity, Guess I’ll just book mark this blog.

    Reply
  3. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time
    a comment is added I get several emails with the same comment.
    Is there any way you can remove people from that service?
    Cheers!

    Reply
  4. You could definitely see your enthusiasm in the article you write.
    The world hopes for even more passionate writers such as you who aren’t afraid to say how they believe.
    All the time go after your heart.

    Reply
  5. Link exchange is nothing else but it is just placing
    the other person’s weblog link on your page at appropriate place and
    other person will also do similar in favor of you.

    Reply
  6. Heya i’m for the first time here. I came across this board and I to find It really useful & it helped me
    out a lot. I hope to provide something again and help others
    such as you aided me.

    Reply
  7. Hi there, i read your blog from time to time and i own a similar one and i was just wondering
    if you get a lot of spam remarks? If so how do you reduce it, any plugin or anything
    you can recommend? I get so much lately it’s driving me mad so any assistance is very much appreciated.

    Reply
  8. After checking out a few of the articles on your site, I seriously like your technique
    of blogging. I saved it to my bookmark website list and will
    be checking back soon. Please check out my website as well and let me know
    what you think.

    Reply
  9. Hi there just wanted to give you a quick heads up and
    let you know a few of the images aren’t loading correctly.
    I’m not sure why but I think its a linking issue. I’ve tried it in two different web browsers and both show the same results.

    Reply
  10. Hi there! This post couldn’t be written any better!
    Reading this post reminds me of my old room mate!
    He always kept talking about this. I will forward this page to him.

    Fairly certain he will have a good read. Thanks for sharing!

    Reply

Leave a Comment