5

TypeScript rationalizes undefined-returning functions

 1 year ago
source link: https://www.infoworld.com/article/3694348/typescript-rationalizes-undefined-returning-functions.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

TypeScript rationalizes undefined-returning functions

TypeScript 5.1, now in beta, soothes pain points by allowing undefined-returning functions to have no return statement, and inferring undefined as the return type of functions with no return expressions in appropriate cases.

By Paul Krill

Editor at Large,

InfoWorld | Apr 21, 2023 7:57 am PDT

Letterpress, TypeScript

Jakub Krechowicz/Shutterstock

TypeScript 5.1, available in a beta release as of April 18, features a variety of coding enhancements including easier implicit returns for undefined-returning functions and allowing unrelated types for getters and setters. Snippet completions for @param JSDoc tags also are featured.

With easier implicit returns for undefined-returning functions, TypeScript 5.1 now allows undefined-returning functions to have no return statement, fixing a situation in which only functions that could have absolutely no return statements were void-returning and any-returning functions. This meant that even if a developer explicity indicated that a function returned undefined, at least one return statement was required.

This was a problem if an API expected a function to return undefined, in which case at least one explicit return of undefined or a return statement and an explicit annotation was needed. This behavior was confusing and frustrating, Microsoft said. Also with TypeScript 5.1, if a function has no return expression and is being passed to something that expects a function to return undefined, TypeScript infers undefined for that function’s return type.

TypeScript 5.1 also now allows completely unrelated types for get and set accessor properties, provided they have explicit type annotations. This follows TypeScript 4.3 allowing a get and set accessor pair to specify two different types.

TypeScript 5.1 is due in a final, production release on May 30; a release candidate is due May 18. Predecessor TypeScript 5.0 was released last month. The 5.1 beta is available through NuGet or via npm:

npm install -D typescript@beta

Other new capabilities and improvements in TypeScript 5.1:

  • TypeScript now provides snippet completions when typing out a @param JSDoc tag in both TypeScript and JavaScript files. This can help cut down on typing and jumping around text as developers document code or add JSDoc types in JavaScript.
  • TypeScript 5.1 avoids performing type instantiation within object types known not to contain references to outer type parameters. This could cut down on many unnecessary computations and reduce type-checking time of Material. UI’s docs directory by more than 50%.
  • When checking if a source type is part of a union type, TypeScript 5.1 first does a fast lookup using an internal type identifier for that source. If the lookup fails, TypeScript checks for compatibility against every type within the union.
  • TypeScript now only runs on Node.js 14.17 and later.
  • Namespaced attribute names now are supported when using the JSX syntax. Also for JSX, TypeScript 5.1 now looks up a type called JSX.ElementType, which specifies precisely what is valid to use as a tag in a JSX element.
  • When TypeScript’s specified module lookup strategy is unable to resolve a path, it now will resolve packages relative to the specified typeRoots.

Paul Krill is an editor at large at InfoWorld, whose coverage focuses on application development.

Follow

Copyright © 2023 IDG Communications, Inc.

Sponsored Links


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK