TypeScript has become so popular over the years that it’s a must-have skill for front-end engineers, and frameworks are built around it.
So, did TypeScript’s emergence and explosion happen by accident? No, it’s inevitable that statically typed languages like TypeScript will become mainstream. Why do you say that?
Let’s start with a question: What is a type?
More specifically, types refer to basic types such as number, Boolean, string, and compound types such as Object and Function. These are abstractions of different things provided by programming languages:
-
Different types of variables take up different amounts of memory: Boolean variables only allocate 1 byte of memory, while number variables allocate 8 bytes of memory. Different types of variables take up different amounts of memory.
-
The number type can do addition, subtraction, multiplication, and division, while the Boolean type cannot. Different types of compound types can use different methods, such as Date and RegExp. Different types of variables can perform different operations on the variable.
Now that we know what a type is, it’s natural to think that the type and the operation we’re doing need to match, which is why we do type checking.
If you can ensure that you only do what is allowed for a type, this is called type safety. For example, if you add, subtract, multiply and divide a Boolean, that’s type-unsafe. If you call exec on a Date object, that’s type-unsafe. Otherwise, it’s type safety.
So, type checking is for type safety.
Type checking can be done at run time or at compile time prior to running. These are two different types. The former is called dynamic type checking and the latter is called static type checking.
Both types of checking have advantages and disadvantages. Dynamic type checking does not retain type information in the source code. Assigning any value to a variable and doing any operation is allowed. Writing code is very flexible. This, however, opens up the potential for type insecurity, such as multiplying and dividing strings or calling the exec method on Date objects, which can only be detected at runtime.
Error: null is not an object; undefined is not a function; null is not an object;
So while dynamic typing is easy to write, it’s easy to hide type mismatches in your code.
Static type checking preserves the type information in the source code, declares variables to specify the type, performs operations on variables to match the type, and is checked at compile time by a special compiler.
Static typing makes writing code a little more difficult, because you have to consider not only the logic the code is expressing, but also the type logic: what type the variables are, whether they match, whether they should be cast, and so on.
However, static typing also eliminates the potential for type insecurity, because when type checking is done at compile time, there are no problems with multiplying and dividing strings or calling the exec method of Date.
As a result, statically typed code, while more complex to write, eliminates the potential for type insecurity in your code.
Knowing the difference between dynamic and static type checking, we can naturally conclude that:
Dynamic typing is only good for simple scenarios, but not for large projects because there are so many hidden bugs in the code that if a type mismatch is reported, it could be a big problem.
While static typing increases the cost of writing code, it is more robust and less buggy.
As a result, large projects are destined to be developed in statically typed languages.
JavaScript was originally designed for form validation in browsers, so it was designed to be dynamically typed and easy to code.
But JavaScript didn’t expect it to be used to develop various projects, such as PC and mobile web pages, React Native cross-app, mini-program, Electron desktop, Node.js server, Node.js tool chain, etc.
When developing large projects, the disadvantages of JavaScript’s dynamically typed language are exposed. The bug rate is too high and robustness is difficult to guarantee. Naturally, there was a strong need for static typing, and TypeScript was born.
TypeScript adds a static typing system to JavaScript, moving from a dynamically typed language to a statically typed language, which allows you to do type checking at compile time to detect type-safety issues ahead of time.
Also, because static typing is added to the code, you can work with the editor for better hints, refactoring, and so on, which is an added benefit.
So is TypeScript’s popularity a fluke? No, I think so, because large projects are destined to be developed in statically typed languages.
conclusion
The type determines the memory size of a variable and the operations that can be performed on it. Ensuring that you only do what to what type is called type-safe, and the way to ensure type-safe is through type checking.
Type checking can be done at run time, called dynamic type checking, or at compile time, called static type checking.
Dynamic typing can be so buggy that large projects are destined to be developed in statically typed languages.
JavaScript itself is a dynamically typed language, and as it is increasingly used to develop large projects, there is a need for static typing. TypeScript fills this need. There’s also the added benefit of better hints and easier refactoring.
So TypeScript’s emergence and current popularity was inevitable.
(Section 1 of TypeScript Type Gymnastic Tips from my nuggets book)