News
New Open Source TypeScript 2.5 Improves Code Refactoring, More
Microsoft last week said new code refactorings highlight the latest open source TypeScript 2.5 release, along with a host of other improvements.
Originally developed and still stewarded by Microsoft after being donated to the open source community (source code is on GitHub), TypeScript is a strict syntactical superset of JavaScript -- with the addition of optional static typing functionality. As it compiles to plain old JavaScript, its tagline says it can be used on any browser and any host on any OS.
"Static types can make it easier to maintain your code by catching bugs early on, making it easier to navigate your projects, giving accurate code completion, and providing handy fixes for when you do make mistakes," said Microsoft's Daniel Rosenwasser in an introductory blog post last week. "By making types optional, you can get the flexibility of plain JavaScript when you need it, all while TypeScript also gives you the option to tighten things up to bring you more type safety."
In detailing the changes to version 2.5, Rosenwasser highlighted the new code refactoring functionality provided by extract method and extract function -- "two new refactorings that make complex rewrites trivial."
Basically, the features -- long available in some other languages -- let developers quickly extract code from existing blocks and quickly turn it into a new function or method.
One of those languages for which the extract functionality has long been available is C#, and its documentation explains more:
Extract Method is a refactoring operation that provides an easy way to create a new method from a code fragment in an existing member.
Using Extract Method, you can create a new method by extracting a selection of code from inside the code block of an existing member. The new, extracted method contains the selected code, and the selected code in the existing member is replaced with a call to the new method. Turning a fragment of code into its own method lets you quickly and accurately reorganize code for better reuse and readability.
Extract Method has the following benefits:
- Encourages best coding practices by emphasizing discrete, reusable methods.
- Encourages self-documenting code through good organization.
- When descriptive names are used, high-level methods can read more like a series of comments.
- Encourages the creation of finer-grained methods to simplify overriding.
- Reduces code duplication.
A comment on Rosenwasser's post said: "We expect extract method and extract function will be available in Visual Studio 2017 version 15.4."
Rosenwasser also detailed other improvements, including guidance about when developers might be doing something wrong -- for example, trying to use JSDoc-style types, which aren't valid in TypeScript. It will "suggest" changes to forestall errors. It also suggests alternative solutions for other issues, such as incorrectly referencing the type of a property off of another type.
Another new feature lets developers simplify try/catch blocks when they don't really care about any thrown errors because the problem can be addressed without even having to touch the error, so it doesn't need to be included as a parameter in the catch block.
Last week's post includes guidance on installing TypeScript 2.5 in different editors and IDEs. It's typically available for installation via NuGet or npm. Manual installation is also available for Visual Studio 2015 and Visual Studio 2017 15.2 or later. Code editors such as Visual Studio Code or Sublime Text can be immediately configured to use the new version 2.5.
Complete details are available in a "what's new" page. Future direction is unveiled in the product roadmap.
About the Author
David Ramel is an editor and writer at Converge 360.