简体   繁体   English

将 Typescript 添加到现有的 React、Webpack 和 Babel 项目中

[英]Adding Typescript to existing React, Webpack and Babel project

I'm trying to add Typescript to a current React, Webpack and Babel project.我正在尝试将 Typescript 添加到当前的 React、Webpack 和 Babel 项目中。 I'd like to be able to have support for filetypes such as, [.js, .ts, .tsx] in my project since I want to successively migrate to Typescript.我希望能够在我的项目中支持诸如[.js, .ts, .tsx]文件类型,因为我想连续迁移到 Typescript。

I've made some progress, but currently, I can't solve this error:我已经取得了一些进展,但目前,我无法解决此错误:

在此处输入图片说明

I'm also not 100% certain if I need to keep Babel, or if I can remove it after this setup is completed.我也不是 100% 确定我是否需要保留 Babel,或者我是否可以在此设置完成后将其删除。

My tsconfig.json looks like this:我的tsconfig.json看起来像这样:

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6", "dom", "esnext.asynciterable", "es2015"],
        "allowJs": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        // "noEmit": true,
        "strictNullChecks": true,
        // "noImplicitAny": false,
        "jsx": "preserve"
    },
    "include": ["src"],
    "exclude": ["node_modules", "build", "scripts", "jest"]
}

And my webpack.conf.js is here: https://gist.github.com/Martinnord/981769791c3e5e3a261af459b81f2733我的webpack.conf.js在这里: https : webpack.conf.js

All help is greatly appriciated since I'm pretty stuck.所有的帮助都非常感谢,因为我很困。

Thank you!谢谢!

webpack.config.js example: webpack.config.js 示例:

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/dist/',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      { test: /\.tsx?$/, loader: 'ts-loader' },
    ],
  },
  resolve: {
    extensions: ['*', '.js', '.jsx', '.ts', '.tsx'],
  },
  plugins: [new webpack.HotModuleReplacementPlugin()],
  devServer: {
    contentBase: path.resolve(__dirname, './dist'),
    hot: true,
  },
}

also, in tsconfig:另外,在 tsconfig 中:

{
  ...
  "jsx": "react",
  ...
}

Full article: here全文: 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM