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.

comments powered by Disqus

Featured

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events