Why Should I Learn TypeScript?

I was one of the first people to be introduced to TypeScript. Microsoft invited me up to their headquarters a few years ago and spent a few days orienting me on this new, excellent, language.

Before I got heavy into JavaScript, I was a C# developer. So they may have invited me to the preview because they were trying to help C# developers make the transition to JavaScript, via TypeScript. In fact, this may be the reason they invented TypeScript in the first place. But this is purely speculation.

Recently, as I was giving a presentation at a Microsoft conference, one of the attendees asked me why it was so important to learn TypeScript? Why is Microsoft pushing it so hard? Why should he learn it?

In this post, I will share a few of the reasons I think TypeScript is fantastic.

TypeScript is Required for Modern SharePoint Framework Web Parts and Extensions

You will have to learn TypeScript (at least the basics) to be able to create anything with the SharePoint Framework. True, you can do almost everything in JavaScript — after you have wired up the essential web part components, but I think it is better to use TypeScript in all of your web part and extension code.

TypeScript Allows for Static Typing

Why is static typing so important? Because it allows you to catch your type errors when you are coding. Or at least, when the solution is bundled. Not at runtime! Here is a simple example.

The following is perfectly valid JavaScript. Your IDE will not let you know there are any problems. But you might get very awkward results, and maybe even an error, at runtime.

Enter the power of static typing available in TypeScript. Notice how the intent of the following code is immediately more clear just by adding strong typing:

Let’s say you leave the company without transferring the knowledge of this method to the developer that proceeds you. As the next developer to come along, I immediately know that this method requires two numbers to be passed in and that the result will be a number. Also, I don’t need any ridiculous commenting to communicate something so basic, because the comments are built into the code!

More importantly, when I try to call the method with the wrong type of data, my IDE will immediately tell me there is a problem.

Cool huh? But maybe that’s not enough to tempt you to embrace TypeScript fully. Here is another reason. Let’s say you want your code to take advantage of interfaces or inheritance or polymorphism, etc.

TypeScript Makes Your Code More Object Oriented

I will use a simple example of interfaces. Let’s say you want to create objects that implement an interface, just like in C# and Java. You can do that with TypeScript

Here is an interface that defines the type of object that will be returned with search results, and is specific to your organization. For example, here is how we want our workforce search results to be composed.

Now that I have defined the interface, I can use it to transform the data that I get from calls to the search API.

In the code above, I am using the array.map function to transform the search results from the search API to implement the IWorkforceSearchResults interface. Using the interface allows me to access all of the properties in the interface, such as Title, FileExtension, etc.

TypeScript also allows you to do cool things like creating mock services.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close