If you're a new developer, then you may have heard about TypeScript, a relatively new programming language that has gained popularity since its release in 2012 by Microsoft.
- What is TypeScript?
- Advantages of TypeScript
- Disadvantages of TypeScript
- Should I learn TypeScript?
- Wrapping up and next steps
Dynamically typed languages also tend to be more flexible, but offer code that is typically less optimized because it must be run first to find out if any bugs are present.
Note: Other examples of dynamically typed languages include Python, PHP, Perl, and Ruby.
What is TypeScript?
TypeScript is an open-source programming language but started off as an internal product at Microsoft in 2010. TypeScript was made publicly available on GitHub in late 2012.
Note: Statically-typed languages include Java, C, and C++. With static typing, the programmer must specify the data type of every variable being declared.
TypeScript was built on 11 design goals:
Microsoft identified 11 key goals that would drive the evolution of TypeScript.
- Statically identify potential errors
- Provide a way to structure larger bodies of code
- Avoid increasing program runtimes
- Produce a language that is composable and easy to reason about
- Avoid adding expression-level syntax
- Make TypeScript consistent, and fully erasable
- Be a cross-platform development tool
- Language changes should not break past versions of TypeScript
Advantages of TypeScript
TypeScript can enforce static typing: Static typing catches potential issues earlier in the development lifecycle by assigning data types to all declared variables
The TypeScript compiler is also highly configurable. There are many options for regulating different aspects of type checking and compilation. The stricter the checks, the more errors can be caught by the TypeScript compiler.
The most common TypeScript error is related to type assignability. The main goal of the type checker is to ensure that values are provided with the correct types.
- React: A single-page application (SPA) that uses a virtual DOM to improve memory and performance
- Vue: Written in TypeScript, easy to integrate into applications progressively
- Angular: Great for quick prototyping, easy testing, and optimizes server performance
Syntactic sugar: Enforcing strict types makes code easier to read because you don't have to think about what type a variable is
Extensive IDE support:
- Visual Studio Code
- ...and more!
Disadvantages of TypeScript
There are a few drawbacks to using TypeScript, but it's up to you to weigh them against the advantages and see if it's right for you.
Here are a few drawbacks to consider:
- Less flexibility: Variables and parameters are less flexible in TypeScript
|Released in 2012||Released in 1995|
|Static typing, strongly typed||Dynamic typing, weakly typed|
|Compiled language, cannot be executed directly in a web browser||Interpreted language, capable of being executed directly in a web browser|
|Learning curve is somewhat steep||Easy to learn|
|Prototyping||Prototyping is available|
|Less documentation||Extensive documentation|
|Best suited for complex or large projects||Best suited for small projects|
|Supports all JS libraries||JS libraries work by default|
|Supports modules, generics, and interfaces||Does not support modules, generics, or interfaces|
|Build setup with npm package||No build setup required|
|Numbers and strings are interfaces||Numbers and string are objects|
Should I learn TypeScript?
If you care more about being able to read the code you write than how long it takes to compile, then TypeScript would have a clear advantage.
Via npm package
First, make sure you have an IDE, the Node.js Package Manager (npm), and the TypeScript compiler. Node.js provides an environment where your TypeScript package can run.
Next, open your command prompt or a terminal and enter the following:
npm install -g typescript
If it was installed correctly, you should be able to check which version you have with
Now, you can use the following command to install TypeScript into your local project as a dependency.
npm install typescript --save-dev
Via Microsoft Visual Studio Code
First, make sure you have Visual Studio installed, along with the ASP.NET web development workload.
- Open a new project in Visual Studio
- Right-click your project node in the Solution Explorer
- Click Manage NuGet Packages
- In the Browse tab, search for Microsoft.TypeScript.MSBuild
- Click Install
In the event that your project does not support Nuget, you can also install the TypeScript extension in Visual Studio by going to Extensions > Manage Extensions.
Wrapping up and next steps
Great! We went over some of the major features of TypeScript, as well as some of the tradeoffs. Hopefully, this brief introduction to TypeScript was informative and has you thinking about your next project. Who knows? You might want to write it in TypeScript.
If you're considering becoming a TypeScript developer, we have plenty of great resources for you to keep expanding your knowledge, so go check them out!
To help you master TypeScript, Educative has created the TypeScript for Programmers learning path.
Continue learning about Typescript on Educative
TypeScript Tutorial: A step-by-step guide to learn TypeScript
A Simple Guide to TypeScript Interfaces: declaration & use cases
Start a discussion
Top comments (1)
What to do you mean by Type mismatches? TS doesn't do type validations, it does type checking. Is a pretty common argument to say that TS should do type checking at runtime, but also is a pretty ridiculous one. The other thing that is kinda weird is the "flexibility" item, mainly because the "flexibility" depends on the types you used. It can be very flexible if you type it that way (and no, I'm not talking about
Either way, I believe the main thing to address with the article is the premise "TS vs JS" ... TS is JS with types on top, is a superset of JS ... so there is no "versus" here, is just if you like your JS with types or not, and even if you work with vanilla JS you can get the benefits of TS using JSDocs.