I found a solution for transferring a create-responsive application from JavaScript to typescript, this method does not require extraction.
- Create a temporary response project using typescript by running the
create-react-app --scripts-version=react-scripts-ts (Note: a global install create-react-app installation is required) - In your own project - under the
package.json file, remove react-scripts - Add
react-scripts-ts to your project by running yarn add react-scripts-ts or, if you use npm, then npm install react-scripts-ts . Or add "react-scripts-ts": "2.8.0" to package.json. - From the project that you created in step 1, copy the files:
tsconfig.json, tsconfig.test.json tslint.json to your own project - As part of our own project, in
package.json , under the script section, changing react-scripts instances of react-scripts-ts (should be under start , build , test and eject - Install the reaction kits by installing the following modules using yarn or npm:
@types/node , @types/react and @types/react-dom . Put in the devDependencies section if you are using package.json. - Change
index.js of index.tsx to index.tsx - In your tsconfig.json file, add the following configuration:
"allowSyntheticDefaultImports": true - for more information
NOTE. Step 7 may require additional modification depending on what you have in the index.js file (if it depends on the JS modules in the project).
Now you can start your project, and it can work, for those of you who receive an error message. You may need an appropriate loader to handle this file type relation to .js files, this is because babel does not know how to load .js. file from .tsx files. What we need to do is change the configuration of the project. I found that doing this without running eject is using the response-app-rewired package . This package allows you to configure the external configuration into which the default project settings will be added / overridden. We will use babel to upload files of this type. Let him switch to:
- Install
react-app-rewired using yarn or npm - In your root folder (where
package.json is located) create a new file called config-overrides.js Put this code in the config-overrides file:
var paths = require('react-scripts-ts/config/paths') module.exports = function override(config) { config.module.rules.push({ test: /\.(js|jsx)$/, include: paths.appSrc, loader: require.resolve('babel-loader'), options: { babelrc: false, presets: [require.resolve('babel-preset-react-app')], cacheDirectory: true, }, }) return config }
Edit the package.json file to use response-app-rewired in the start/start-js , build , test and eject scripts, as shown in the project readme file . For example, "test": "react-app-rewired test --scripts-version react-scripts-ts --env=jsdom" .
Now you can start the project, and the problem should be solved. You may need additional modifications depending on your project (additional types, etc.).
Hope help
As suggested here in the comments, you can define: "compilerOptions": { "allowJs": true} in your tsconfig.json file, so you can mix JS and TS without reacting via app-app. Thanks for mentioning this!
UPDATE: creation-reaction-application version 2.1.0 supports typewriting, so for those of you starting from scratch, you can use it to create a new typewriter application, and according to the documentation, this should be done using the following commands:
$ npx create-react-app my-app
For existing projects, after upgrading to version 2.1.0, add the following packages depending on your project with the following command:
$ npm install --save typescript @types/node @types/react @types/react-dom @types/jest
and then you can just rename .js to .ts files.