Migrate Create React App from JavaScript to TypeScript by using ts-migrate

ts-migrate is a Node.js tools to convert from JavaScript to TypeScript. We can easy to migrate your React appl […]

広告ここから
広告ここまで

目次

    ts-migrate is a Node.js tools to convert from JavaScript to TypeScript.

    We can easy to migrate your React application written by JavaScript to use it.

    Prepare

    In Create React App, we need to get the following files to use TypeScript.

    • tsconfig.json
    • src/react-app-env.d.ts

    We can easy to get creating a new dummy React app.

    $ npx create-react-app test --typescript
    $ mv test/tsconfig.json ./
    $ mv test/src/react-app-env.d.ts ./src

    Installing npm packages for TypeScript

    At least, we need add these packages.

    $ npm i -D @types/react @types/react-dom @types/node @types/jest typescript

    Run ts-migrate

    Using ts-migrate, we can rename all js/jsx files to ts/tsx.

    $ npx ts-migrate rename ./

    And update all code to be able to run in TypeScript.

    $ npx ts-migrate migrate ./

    Fix errors of TypeScript

    Depends on any dev tools like prop-types, sometimes, ts-migrate will remain any TS errors.

    We should run npm run build and fix all errors.

    広告ここから
    広告ここまで

    Random posts

    Home
    Search
    Bookmark