DEV Community

Daniel Hauser
Daniel Hauser

Posted on

6 3

Convert a react app from flow to typescript without losing git history

start with creating a new branch

git checkout -b "moving-from-flow-to-ts"

Rename files from js to ts[x]

Lets start with renaming all .js files to .ts with the following command:

ls ./src/**/*.js | 
    while read line; do git mv -- $line ${line%.js}.ts; done;

Then, assuming all files importing react are jsx files, rename them from .ts to .tsx

find ./src -type f -name "*.ts" |
    xargs grep 'import React[ ,]' |
    cut -d: -f1 |
    uniq | 
    while read line; do git mv -- $line ${line%.ts}.tsx; done;

Commit to save the new file names

git commit -m "rename js files to ts and tsx"

Move to typescript

Remove flow-bin from package.json

npm uninstall flow-bin

Then, install and initialize typescript

npm install --save-dev typescript @types/react @types/react-dom && tsc --init

Note: If you use decorators, enable the experimentalDecorators flag in tsconfig.json before running the next step

Commit to save the changes

git commit -m "replace flow with typescript"

Convert all flow types to typescript types

Fix types in all tsx files

npx flow-to-ts --inline-utility-types --write -o tsx ./src/**/*.tsx

Fix types in all ts files

npx flow-to-ts --inline-utility-types --write -o ts ./src/**/*.ts

Next step

Run tsc --noEmit to see compilation errors, and fix them manually

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (1)

Collapse
 
sovcik profile image
Jozef Sovcik

Hi, thanks for the post. For future use pls modify it to include @khan/flow-to-ts as original flow-to-ts has been deprecated.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay