In recent months I have abandoned JavaScript altogether and used TypeScript entirely for front-end development (I only use JS in class).

First, the advantages:

  1. Bugs are significantly reduced, the previously encountered XXX empty problem is almost no longer present, and type-related bugs are linearly reduced.
  2. Applications are more controllable, and when you need to constrain some code, it’s easy to do so using types, such as the diaplayName force in React.
  3. Documentation is more convenient, before to open the browser to see the document, now directly view the definition of the basic understand.

The disadvantages:

No. Ha ha.

What about the JS front end now?

Don’t panic, TS code is similar to JS code. After learning JS, you only need to learn the type declaration to master TS.

If your company’s project currently only supports JS, it doesn’t matter. Just add a TS-loader or awesome-typescript-loader to support typescript. Ts and JS can coexist.

Then you can gradually replace JS with TS to achieve a perfect transition.

Why TypeScript is good?

If you haven’t started studying TS yet, it’s probably because you’re afraid of IT. Ask how you feel about using the TS front end, and almost no one says they regret it. So this kind of concern is totally unnecessary.

So why is TS so good? Now let’s explain it in theory.

  1. What’s the biggest fear of writing code? A code error is a bug.
  2. How to avoid bugs? Run the code to see the results, or add various tests.
  3. Unit testing is not popular on the front end, so you can only run code to see the results (i.e. refresh the page and click to see if it works)
  4. But when your front-end application is really big, you can’t just click on all the pages every time you change the code because there are so many pages.
  5. So the front end is modular, so that a code change affects as few pages as possible. But even then, you won’t be able to run all the code with a mouse click test, because you might also need to test many different accounts.
  6. It’s too much trouble to do that. Is there any way I can quickly tell if the code is buggy or not?

This is an important question: is there any way I can quickly tell if the code is buggy?

To illustrate how typing solves this problem, let’s start with the simplest type: positive and negative numbers.

We divide real numbers into three types: positive, negative and 0.

Then look at this equation:

28937829 * -1239282 = 35862130598778
Copy the code

You are smart enough to see that this equation is wrong. Why is that? Because a positive multiplied by a negative is a negative. So we don’t even have to run this multiplication to know that this is not right.

That’s the type benefit.

A type gives you a “rough idea” of whether the code is correct

TS is simply adding type declarations to JS so that we know if the code is “roughly” correct.

Plus, it’s fast, so fast that you can just change the code and TS can tell you if the code is “roughly” correct.

Avoid a lot of bugs.

You can improve the quality of your code with a little bit of time, so why not?

I heard TS is only suitable for large projects?

No, as long as there is a bug JS project, can use TS instead of JS to reduce bugs.

So whether it is a small project or a large project, it is necessary to use TS.

What if TS dies down in a few years?

That’s a good question. The front-end is growing so fast that many things die after a few years of popularity, making it hard to hire people to maintain them later (e.g. AngularJS 1).

But TS doesn’t have this problem. Why is that?

Because TS is now supported by all three front-end frameworks:

  1. Angular has long supported TypeScript and dropped JS from its name: AngluarJS -> Angular. Even the examples in the Angular getting started documentation are TS versions by default. It’s not impossible to write Angular in JS, it’s just “weird” why anyone would use JS when there are better TS.
  2. Vue 3.0 was rewritten with TS, and even dropped the planned class API in order to better support TS.
  3. React’s initial support for TS was also very silky. React doesn’t bind strongly to TS though.

If one year TS is not popular, the maintainers of the framework above will have an upgrade plan for you in advance, so you don’t have to worry too much.

After all, trees are good for shade.

JS for nothing?

No, No, No, TS contains all the syntax for JS, so when you use TS, you are still using JS.

In other words, JS is still alive, we just don’t use JS alone anymore.

conclusion

Learn TypeScript quickly, it will soon be a plus or even a requirement for job interviews at top-tier Internet companies.

counter

If you have something to refute, you are welcome to refute, but please give a good reason to refute, meaningless siding and flooding comments will be deleted, so that our discussion will be more meaningful.

Comments made without reading the article will also be deleted.