Skip to main content

Introduction to TypeScript

JavaScript was originally created to make web pages live. Due to its ease of use, it gained popularity and began to be used on almost all platforms. JavaScript code is built on scripts, which are supposed to be not big and fast. The developers did not think that JavaScript would be used to write applications with a lot of code. In this connection, large applications written in JavaScript have problems. Here are some of them:

  1. Due to the interpretability of the language, you can find out about the error only after executing the code;
  2. The complexity of using the practices of object-oriented programming;
  3. Dynamic typing.

To solve such problems, a programming language was created TypeScript.

TypeScript#

TypeScript - a programming language introduced by Microsoft in 2012 and positioned as a web application development tool that extends JavaScript. TypeScript is backward compatible with JavaScript and compiles to the latter. In fact, once compiled, a TypeScript program can be executed in any modern browser or shared with the Node.js server framework. TypeScript code looks almost the same as JS code, and if you have experience in frontend development, learning TypeScript is easy enough. Especially considering that you can write JS code directly in TS scripts.

History#

The development of TypeScript began at the end of 2012. Although it originated in the company Microsoft, and its actual creator is the programmer Anders Hejlsberg, also known as the creator of such languages ​​as Delphi, C # , but this project immediately began to develop as OpenSource. And from the very beginning, the new language began to spread rapidly due to its flexibility and productivity. A lot of projects that were written in JavaScript began to be ported to TypeScript. The popularity and relevance of the ideas of the new language has led to the fact that a number of these ideas will subsequently become part of the new JavaScript standard. And the new version of one of the most popular web frameworks - Angular 2/4/5 / 6 is written entirely in TypeScript jointly by Microsoft and Google.

Benefits TypeScript#

TypeScript brings many conveniences to the world of JavaScript, let's take a look at its main features.

Strong typing#

TypeScript is a strongly typed and compiled programming language. JavaScript is dynamically typed. Strong typing is one of the options for working with data types, which means that the following conditions are met:

  • The data object always has a strictly defined type;
  • A variable can only be assigned strictly the same data type as the variable;
  • Operations require parameters of strictly defined types;
  • Implicit type conversion is not allowed.

Example JavaScript:

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^

The variable fruit can be either a string, a number, or a function. No error occurs. In a strongly typed language, such code will fail compilation.

Example TypeScript

let fruit: string = 'banana'
fruit = 666
console.log(fruit)

The syntax : string before fruit means that the variable is assigned the type string (string). In the next line, we assign a number to it, which is not allowed.

Compilation#

One of the annoyances you can run into when developing in JavaScript is a typo or accidental redefinition of a global variable. That can be found only at the stage of program execution. And this clearly does not save development time.

TypeScript checks the code for errors during compilation, which will obviously speed up the identification of such troubles.

OOP#

TypeScript implements many of the concepts that are inherent in object-oriented languages, such as inheritance, polymorphism, encapsulation and access modifiers, and so on.

Large programs#

TypeScript makes it faster and easier to write large, complex programs, and thus easier to maintain, develop, scale, and test than in standard JavaScript.

OpenSource#

TypeScript is being developed as an OpenSource project and, like many projects, is hosted on github. In addition, it is cross-platform, which means that we can use both Windows and MasOS or Linux for development.

Cons of TypeScript#

Developers love this language, and some large projects are already switching to it. For example, the popular Angular.JS framework. But this is still not enough for it to become as popular as JavaScript. This is because it is more expensive and time-consuming to develop a TypeScript web application.

Especially if you need to use some library or framework that is not ported to TS. In this case, developers will have to independently describe the signatures (indicate data types) of all functions and methods - a rather lengthy process, given the size of modern libraries.

Also, the threshold for entering TypeScript is higher - in order to take advantage of its benefits, it is important to know data types and object-oriented programming.

Whether to use TypeScript#

Now that you know what TypeScript is, I'll try to tell you when you need to and in which you shouldn't use it.

My reasoning is very simple: TypeScript should always be used if you know that the project will grow and need to be maintained. Should I translate already written projects? - I cannot give you an exact answer, usually in such projects it is difficult to find time for refactoring, and even more so for switching to TypeScript. However, it is still worth thinking about it, because you get typing and the ability to use modern standards.

In the event that you know in advance that the project has certain frameworks of the code base and they are small, then the implementation of TypeScript will only bring you support for modern ECMAScript capabilities. However, remember that you can always switch from JavaScript to TypeScript, but on the contrary, you will have to manually delete all types, or use the generated files.

Questions#

Now we are ready to learn TypeScript with you, but in order to understand how much you learned this lesson, take the test in the mobile application в нашей школы по этой теме.

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Links#

  1. TypeScript Lang
  2. Thoughts out loud about TypeScript
  3. The basics TypeScript
  4. Сanonium

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


IIo3iTiv

📖

Dmitriy Vasilev

💵

Become a Patron!