TypeScript: A Superset of JavaScript
Sunil Sharma
Turning frowns into clickable smiles! ?? Enterprise UX Analyst on a mission to make software so user-friendly, even your coffee machine will ask for feedback.??
Multi-paradigm: functional, generic, imperative, object-oriented
In this article, I have shared the essential information about TypeScript programming language. This article contains a basic overview, brief history, features and applications of TypeScript.
TypeScript is getting more popular in Full-stack development. In terms of popularity, in the last two years TypeScript pulled ahead of Ruby, Shell, and C. In the latest State of Octoverse report, which tracks programming language popularity over time, TypeScript ranks seventh.
Why TypeScript
When JavaScript was developed then JavaScript development team introduced JavaScript as a client-side programming language. But when people were using JavaScript then developer get to know that JavaScript can be used as a server-side programming language also. But When JavaScript was growing then the code of JavaScript became complex and heavy. Because of this, JavaScript was even not able to full fill the requirement of Object-oriented programming language. This prevents JavaScript from succeeding at the enterprise level as a server-side technology. Then TypeScript was developed by the development team to bridge this gap.
Overview
TypeScript is an open-source pure object-oriented programming language. It is a strongly typed superset of JavaScript which compiles to plain JavaScript. It contains all elements of the JavaScript. It is a language designed for large-scale JavaScript application development, which can be executed on any browser, any Host, and any Operating System. The TypeScript is a language as well as a set of tools. TypeScript is the ES6 version of JavaScript with some additional features.
TypeScript cannot run directly on the browser. It needs a compiler to compile the file and generate it in JavaScript file, which can run directly on the browser. The TypeScript source file is in “.ts” extension. We can use any valid “.js” file by renaming it to “.ts” file. TypeScript uses TSC (TypeScript Compiler) compiler, which convert TypeScript code (.ts file) to JavaScript (.js file).
TypeScript VS JavaScript
- JavaScript is a scripting language which helps you create interactive web pages whereas TypeScript is a superset of JavaScript.
- TypeScript code needs to be compiled while JavaScript code doesn’t need to compile.
- TypeScript supports a feature of prototyping while JavaScript doesn’t support this feature.
- TypeScript uses concepts like types and interfaces to describe data being used whereas JavaScript has no such concept.
- TypeScript is a powerful type system, including generics & JS features for large size project whereas JavaScript is an ideal option for small size project.
Brief History
- The TypeScript was first made public in the year 2012
- After two years of internal development at Microsoft. TypeScript 0.9, released in 2013
- Additional support for generics TypeScript 1.0 was released at Build 2014
- In July 2014, a new TypeScript compiler came which is five times faster than the previous version
- In July 2015, support for ES6 modules, namespace keyword, for, of support, decorators
- In November 2016, an added feature like key and lookup types mapped types, and rest
- On March 27, 2018, conditional types, the improved key with intersection types supports added in the TypeScript
- TypeScript 3.0 was released on July 30, 2018, bringing many language additions like tuples in rest parameters and spread expressions, rest parameters with tuple types, generic rest parameters and so on
- Stable release: TypeScript 3.9.7, released on July 6, 2020
- Preview release: TypeScript 4.0 Beta, released on July 7, 2020
Features
1. Variable Checks
TypeScript helps developers quickly figure out the purpose of a variable within the code (you can quickly check the variable name and the data it contains). TypeScript can actually suggest available properties in functions, classes, or components.
Being able to quickly lookup a variable is important because it reduces the likelihood of calling the wrong function or accidentally skipping a variable declaration — fixing bugs like these is cumbersome and takes time.
2. Bug Checks
TypeScript is a static language, meaning it performs type checks upon compilation, flagging type errors and helping developers spot mistakes early on in development.
According to University College London, static type systems can reduce the number of bugs by up to 15%. While that may not seem like a lot at first glance, when you consider large codebases, a drop like that can mean saving hours of precious development time.
3. Readability
Clear and readable code is easy to maintain, even for newly onboarded developers. Because TypeScript calls for assigning types, the code instantly becomes easier to understand and work with. As such, TypeScript code is essentially self-documenting, which allows distributed teams to work much more efficiently without having to spend a lot of time familiarizing themselves with a project.
4. Structural Typing
While it might be initially cumbersome to those coming from a JavaScript background, structuring the code using types and interfaces lets developers think ahead in terms of design. Again, this is particularly important in large-scale applications.
JavaScript gives developers more flexibility in this regard, but with that flexibility comes the risk of messing the code up, so it’s essentially up to the developer and their experience with JS to ensure the structure is controlled right from the start.
5. IDE Support
TypeScript’s integration with editors (equipped with an autocomplete feature) makes it much easier to validate the code thanks to context-aware suggestions. TypeScript can determine what methods and properties can be assigned to specific objects, and these suggestions tend to increase developer productivity (and decrease the need to check the props).
7. JavaScript Libraries & Tooling
Even though JavaScript libraries can be used freely with TypeScript, they don’t have type definitions by default, so you have to add them manually. The upside is that you can use the community-built repo Definitely Typed to conveniently define types and enjoy JS libraries.
Applications
TypeScript is compiled to JavaScript. Therefore, TS can be used anywhere JS could be used: both the frontend and the backend.
JavaScript is the most popular language to implement scripting for the frontend of apps and web pages. Thus, TypeScript can be used for the very same purpose, but it shines in complex enterprise projects on the server-side.
At Serokell, most of our web frontend is implemented in TypeScript.
>>When it makes sense to use TypeScript:-
- When you have a large codebase.
- When your team’s developers are already accustomed to statically-typed languages.
- TypeScript can serve as a replacement for Babel.
- When a library or framework recommends TypeScript.
- When you really feel the need for speed.
Thanks for the article, IMHO JS code has more readability than TS