Migrating create-react-app from javascript to typescript

前端 未结 4 1425
迷失自我
迷失自我 2020-12-12 22:24

I started a react project using create-react-app few months ago and I\'m interesting in migrating the project from Javascript to Typescript.

I saw that there is a wa

4条回答
  •  抹茶落季
    2020-12-12 22:51

    You will need to eject a configuration in order to do that.

    After ejecting you need to perform the following steps:

    1. Add typescript extensions tsx and ts to the extensions table in the webpack configs (dev and prod).
    2. Add ts-loader. Here is an example

      {
        test: /\.(ts|tsx)$/,
        include: paths.appSrc,
        use: [
          {
            loader: require.resolve('ts-loader'),
          },
        ],
      },
      
    3. Change eslint to tslint

    4. Add source-map-loader to get a possibility to debug Typescript files.

      {
        test: /\.js$/,
        loader: require.resolve('source-map-loader'),
        enforce: 'pre',
        include: paths.appSrc,
      }
      
    5. Create a Typescript config file and remember to set allowJs to true.

提交回复
热议问题