MacMusic  |  PcMusic  |  440 Software  |  440 Forums  |  440TV  |  Zicos
typescript
Recherche

What is TypeScript? Strongly typed JavaScript

vendredi 6 décembre 2024, 10:00 , par InfoWorld
Originally developed by Microsoft as a way to adapt JavaScript as a tool to build enterprise-scale applications, TypeScript has become a widely popular programming language that combines JavaScript syntax with strong typing and object-oriented programming. This article gets you started with an overview of TypeScript, offering a brief history, a rundown of what’s both different and similar to JavaScript, and an explanation of why TypeScript is an attractive language to use. We’ll also point you toward resources for learning how to write TypeScript code.

What is TypeScript?

TypeScript is a variation of the popular JavaScript programming language that adds features that are important for enterprise development. In particular, TypeScript is strongly typed—meaning that the programmer can declare variables and other data structures to be of a specific type, like a string or a boolean, and TypeScript will check the validity of their values. This isn’t possible in JavaScript, which is loosely typed.

TypeScript’s strong typing enables a variety of features that increase developer efficiency, especially when dealing with enterprise-scale codebases. TypeScript is compiled, rather than interpreted like JavaScript, so errors can be caught before execution; IDEs that perform background incremental compilation can spot such errors during the coding process.

Despite this key difference from JavaScript, TypeScript can still be executed anywhere JavaScript can run. That’s because TypeScript compiles not to a binary executable, but to standard JavaScript.

TypeScript vs. JavaScript

TypeScript is a superset of JavaScript. While any correct JavaScript code is also correct TypeScript code, TypeScript also has language features that aren’t part of JavaScript. The most prominent feature unique to TypeScript—the one that gave TypeScript its name—is, as noted, strong typing: a TypeScript variable is associated with a type, like a string, number, or boolean, that tells the compiler what kind of data it can hold. In addition, TypeScript supports type inference, and includes a catch-all any type, which means that variables don’t have to have their types assigned explicitly by the programmer.

TypeScript is also designed for object-oriented programming, whereas JavaScript is not. Concepts like inheritance and access control that are not intuitive in JavaScript are easy to implement in TypeScript. In addition, TypeScript allows you to implement interfaces, a largely meaningless concept in the JavaScript world.

That said, there’s no functionality you can code in TypeScript that you can’t also code in JavaScript. That’s because TypeScript isn’t compiled in a conventional sense—the way, for instance, C++ is compiled into a binary executable that can run on specified hardware. Instead, the TypeScript compiler transpiles TypeScript code into JavaScript. On the Toptal blog, Daniel Monesi compares functionally equivalent TypeScript and JavaScript code so you can get a sense of the differences.

This transpiled JavaScript can then be run anywhere any JavaScript code can run, from a web browser to a server equipped with Node.js. But that of course just raises the question: if TypeScript is, in the end, just a fancy way to generate JavaScript code, why bother with it? To answer that question, we need to look at where TypeScript came from and what it’s used for.

A brief history of TypeScript

TypeScript was released as open source in 2012 after being developed within Microsoft. (The software giant remains the project’s steward and main developer.) A ZDNet article from the time offers an intriguing look into why that happened: “It turns out one of the big motivations was the experience of other teams at Microsoft who were attempting to develop and maintain Microsoft products in JavaScript.”

At the time, Microsoft was trying to scale up Bing Maps as a competitor to Google Maps, as well as to offer web versions of its Office suite—and JavaScript was the primary development language for these tasks. But that was easier said than done: Although it has spread to an incredible number of niches, JavaScript was originally developed for interactive web design, and one of its main virtues is that it’s easy to write workable JavaScript code quickly. But enterprise software development has different needs, favoring robustness and maintainability over speed.

In essence, Microsoft’s developers found it difficult to write applications on the scale of the company’s flagship offerings using JavaScript. So they developed TypeScript to make it easier to build enterprise-level applications to run in JavaScript environments. This is the spirit behind the original tagline for the language on the TypeScript project site: “Safety that scales.”

Why should I use TypeScript?

So, what concrete benefits does TypeScript offer over JavaScript?

Foremost is the fact that TypeScript is object-oriented and strongly typed. We’ve touched on this already, but it’s key to TypeScript’s value proposition for larger development teams. It’s easy for a developer to look at TypeScript code someone else wrote and get a sense of the parameters that functions expect and what they return, as well as what kind of data the code will be pulling in from API endpoints and database queries. Data processing and consistency is much easier to do correctly when you’re creating your own custom types, classes, and interfaces.

Another benefit is that IDEs can significantly accelerate TypeScript development, which is not so much the case with JavaScript. In part because of its object-oriented and strongly typed nature, TypeScript has access to many of the productivity enhancements enterprise developers expect from IDEs. IDEs can more easily autocomplete text or offer pop-up menus for guidance because they can “know” the structure of TypeScript objects (both the ones you write and the ones in third-party libraries). Most IDEs can also transpile TypeScript code on the fly, which provides valuable continuous error checking as you write code.

It’s also important to note that TypeScript is still (mostly) JavaScript. So far, we’ve talked about the ways JavaScript isn’t necessarily a great fit for enterprise development. But why would you choose TypeScript over Java or.NET or some other platform designed for enterprise development from the ground up? Well, there are lots of benefits to JavaScript too: It is easy to write JavaScript code quickly; it runs everywhere; and JavaScript engines have received investment from the big three browser makers, so it tends to run pretty quickly. TypeScript delivers all of JavaScript’s portability and performance benefits because it transpiles to ordinary JavaScript code.

TypeScript is particularly attractive to shops that have already invested in JavaScript for their codebase but want to make that codebase easier to maintain and extend. Since valid JavaScript is also valid TypeScript, you can gradually add TypeScript features to your existing JavaScript code rather than having to rewrite everything from scratch in a new language; Bhekani Khumalo has a great blog post showing how to do this. Additionally, your current stable of JavaScript developers should be able to get up to speed on TypeScript quickly, given the similarity between the two languages.

Oh, and TypeScript has another neat trick up its sleeve: You can set the compiler to target a specific JavaScript runtime environment, browser, or even language version. Since any well-formed JavaScript code is also TypeScript code, you could, for instance, take code written to the latest version of the ECMAScript spec, which might include cutting edge or experimental language features, and transpile it into JavaScript code that would be compliant with legacy versions of the language.

How to install TypeScript

Ready to start playing with TypeScript? Installing the language is easy. If you already use Node.js on your development machine, you can use NPM, the Node.js package manager, to install it. The official TypeScript for programmers tutorial will walk you through the process.

You can also install TypeScript as a plug-in to your IDE of choice, which will give you the tooling advantages we’ve talked about and also take care of the process of compiling TypeScript to JavaScript. Since TypeScript was developed by Microsoft, it’s unsurprising that there are high-quality plug-ins available for Visual Studio and Visual Studio Code. But as an open source project, TypeScript has been adapted everywhere, ranging from open source IDEs like Eclipse to venerable text editors like Vim. The whole project can also be browsed and downloaded from GitHub.

TypeScript tutorials

Ready to go deeper? Get up to speed with these TypeScript tutorials:

TypeScript for JavaScript Programmers is the official introductory tutorial for TypeScript and guides you through the process of installing TypeScript if you haven’t already.

The Beginner’s TypeScript tutorial by Matt Pocock is a more detailed tutorial for new learners, though it assumes you are already familiar with JavaScript.

This Visual Studio Code tutorial demonstrates what the IDE can add to your TypeScript development productivity.

TypeScript Tutorial for Beginners: The Missing Guide is a thorough introduction that will be useful even if you have fairly limited JavaScript experience.

If you’re looking to use TypeScript with the popular React JavaScript framework, Hands on React has a good tutorial for you.

Better Stack has a good introduction to using TypeScript with the popular Node.js framework. (Node.js is also beginning to directly support code written in Typescript, rather than requiring it to be transpiled to JavaScript.)
https://www.infoworld.com/article/2257305/what-is-typescript-strongly-typed-javascript.html

Voir aussi

News copyright owned by their original publishers | Copyright © 2004 - 2024 Zicos / 440Network
Date Actuelle
mer. 18 déc. - 20:03 CET