No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home Javascript

The story of migrating 70,000 lines of JavaScript code to TypeScript

npn by npn
December 15, 2020
in Javascript, React
Reading Time: 8 mins read
0
The story of migrating 70,000 lines of JavaScript code to TypeScript
0
SHARES
506
VIEWS
Share on FacebookShare on Twitter

Contents

  • 1 Introduction
    • 1.1 READ ALSO
    • 1.2 Configuring VS Code for Node/JavaScript Development
    • 1.3 How does Nodejs solve the problem of high concurrency?
    • 1.4 Project Outline
  • 2 Why didn’t you adopt TypeScript in the first place?
  • 3 Why did you decide to move to TypeScript?
  • 4 Advance preparation
    • 4.1 Identify requirements from source and screen
    • 4.2 Rescheduling
  • 5 Migration procedure
    • 5.1 Create a project on a zero basis
    • 5.2 Gradually transition
    • 5.3 Test
    • 5.4 Refactoring
  • 6 The good thing about TypeScript that I felt after trying it
  • 7 What I should have done
  • 8 What was adopted at the time of TypeScript migration
  • 9 It was hard
  • 10 Conclusion
  • 11 Digression
  • 12 Conclusion
5/5 - (1 vote)

Introduction

In the project I’m working on, I’ve experienced migrating over 70,000 lines of code from JavaScript to TypeScript.
We will share the preparations and procedures for the transition, and what we felt when we actually tried

READ ALSO

Configuring VS Code for Node/JavaScript Development

Configuring VS Code for Node/JavaScript Development

August 2, 2021
1.3k
How does Nodejs solve the problem of high concurrency?

How does Nodejs solve the problem of high concurrency?

July 18, 2021
1.3k

Project Outline

  • Full stack web application
  • Frontend: JavaScript + React
  • Backend: Node.js + Express.js + GraphQL

Why didn’t you adopt TypeScript in the first place?

When selecting the technology in advance, there was a discussion about whether to use TypeScript.
At that time, I decided not to use it for the following reasons.

  • Since I was used to developing JavaScript, I had a strong image that TypeScript type definition = troublesome.
  • I thought that “speed is life”, and I thought that using TypeScript would reduce development efficiency.
  • At the beginning of the project, I was the only development member, so I felt that JavaScript was sufficient.
  • At that time, I didn’t expect the project to have that much source.

Why did you decide to move to TypeScript?

As development progressed and I wrote 70,000 lines of code, I decided to move to TypeScript for the following reasons.

  • There were too many bugs in the development stage
    • In particular, there were many errors related to undefined/null (Cannot read property ○○ of undefined).
    • There were many simple type errors
  • As the source bloated, I couldn’t keep up with what was going on.
    • React component propsand function arguments are not clearly defined
    • I can put anything in the object type
  • Other members will join the place where I was developing alone
    • The source was so disturbed that it was difficult for other members to catch up.
    • On the contrary, it was difficult to read the sources written by other members.

Advance preparation

Identify requirements from source and screen

Since the behavior should not change from that of JavaScript, we have identified the functional requirements from the existing source and the actual screen.
It was useful later to make a note of detailed specifications that were not written in the original requirements.

Rescheduling

First, we had to see how much code we were migrating.
I think there are many ways to do this, but I used VS Code Counter, an extension of VS Code.
Just run it and Markdown will output the number of files and lines of code for each language source.

Ideally, there will be no JavaScript code, so almost all JavaScript files will be migrated.
Rescheduled according to the number of files and lines of code.

Migration procedure

When you’re ready, it’s time to move to TypeScript. There seems to be a TS migration tool called
ts-migrate, but I was scared and couldn’t use it.

Create a project on a zero basis

Since the source is managed on GitHub, I started by separating the branch. I’m using Create React App for
this React app, so

npx create-react-app my-app --template typescript

I created a TypeScript-based project from scratch and moved only the files.
Regarding the base part, it was easier to proceed from scratch than to modify it.

Gradually transition

Setting with tsconfig allowJs:true, JavaScript and TypeScript are compatible.
I gradually moved from the common part source to TypeScript.
As a specific procedure,

  1. Change the extension to ts / tsx
    • Tsx if you use React components, ts otherwise
  2. Give molds as needed
    • If you use tslint, the error part turns red
    • Do not define more types than necessary because of type inference
  3. If you can’t help it, make it a anymold for the time being and refactor it later.
    • anyAllow as a first-aid only if the type is difficult to define
    • as any‘s easy to cause bugs, so you need to refactor it later.

In the case of JavaScript, it is necessary to think about the type for the object that was ambiguous, so it was necessary to
reconsider the specifications when creating it.
I felt that the program became more sophisticated as I moved to TypeScript.

ADVERTISEMENT

Test

After the development was finished, I tested it while comparing it with the screen before the migration.
The behavior may have changed slightly in small areas, so meticulous testing is required.

Refactoring

Finally asarrow anywill continue to lose because of the cause of the bug.
Ideally strict:true, allowJs:falseit works with tsconfig.
As you can see in this article,
be aware that depending on how you write it, it can break the type safety of TypeScript.

The good thing about TypeScript that I felt after trying it

  • Development efficiency has improved considerably
    • Powerful input completion (no spelling mistakes)
    • Type checked while typing
    • You can check the variable type by mouse over
    • You can check the type of function arguments and return values
  • Bugs can be prevented
    • Static type checking at compile time
  • You can write the source beautifully
    • Type definition can be treated as a simple document
    • Type ambiguity is eliminated, making it reasonably easy for anyone to see
    • Easy to catch up with new development members (he said it was easy to understand)

What I should have done

  • Systematization of automated testing by Jest
    • Static type checking alone can’t prevent all bugs, so I should have implemented unit testing with Jest.
    • I didn’t have enough time to implement it
  • TypeScript study session
    • Some members were confused by the TypeScript-specific writing style.
    • Holding a TypeScript study session may have improved overall development efficiency

What was adopted at the time of TypeScript migration

  • GraphQL Code Generator
    • A tool for generating TypeScript type definitions from GraphQL schema
    • React + Apollo will generate a type-safe function
    • Thanks to this tool, TypeScript, and GraphQL go great together.
  • TypeORM
    • TypeScript OR mapper
    • You can get data from DB without writing SQL
    • Can be implemented type-safer than writing SQL directly
  • routing-controllers
    • A library that allows you to write Express controllers based on TypeScript classes
    • You can also validate and sanitize
  • AWS CodeBuild
    • By using TypeScript, it took longer to build, so I changed it to build in the cloud
    • CodePipeLine automates a series of steps to reduce deployment mistakes

It was hard

  • It took a long time anyway
  • If the source before migration is rough, you can not write the type definition neatly
    • In some cases, it needed to be remade
  • Learning TypeScript was hard
    • The first one spits out an incomprehensible error
    • It was hard to find out how to make it the best practice
    • Difficult to understand tsconfig settings and each item
    • Concepts that decorator, class-based, and JavaScript don’t have come out
  • The type definition was not prepared in the library you are using
    • I had to create my own type definition file (d.ts)

Conclusion

By migrating 70,000 lines of JavaScript code, development has become overwhelmingly advanced.
The migration itself was hard, but I like writing code so it was a lot of fun.

I think there are various ways of thinking about “should TypeScript be adopted”, but
if it is used for a certain period of time, I think that there is no loss in adopting TypeScript from the beginning.

Digression

In another case, I was frustrated trying to migrate a project written in Javascript + Vue to TypeScript.
Vue had a habit of writing originally, and even with TypeScript, type inference did not work
well, so I had the impression that it was not compatible with TypeScript compared to React.
It seems that TypeScript is fully supported in Vue 3.x, so I’m looking forward to it.

Conclusion

I’ve written a lot of things I’m thinking about, but as a person who loves TypeScript, I’d be happy if I could convey its charm as much as possible.
If you have the image that “TypeScript is troublesome”, I would like you to try TypeScript once.
Why not consider migrating to TypeScript before your JavaScript project grows too large?

Author: tonio0720 from quiita.

Tags: expressreactTypeScript
ShareTweetShare
Previous Post

TypeScript 4.1 adopts literal template types

Next Post

The interest of next/image from Next.js 10

npn

npn

Related Posts

Configuring VS Code for Node/JavaScript Development
Javascript

Configuring VS Code for Node/JavaScript Development

August 2, 2021
1.3k
How does Nodejs solve the problem of high concurrency?
Javascript

How does Nodejs solve the problem of high concurrency?

July 18, 2021
1.3k
Migrate from Hot Reload to Fast Refresh
React

Migrate from Hot Reload to Fast Refresh

December 16, 2020
1.5k
[React] React installation for Mac
React

[React] React installation for Mac

December 16, 2020
319
Npm module: a backdoor and ambush questions
Javascript

Npm module: a backdoor and ambush questions

December 16, 2020
311
NPM: three packets contained malicious code
Javascript

NPM: three packets contained malicious code

December 16, 2020
193
Next Post
The interest of next/image from Next.js 10

The interest of next/image from Next.js 10

Discussion about this post

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner