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.
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
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.
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.
How Can We Help? Find out how Lone Peak Software can help with Office 365, SharePoint and Azure.