Wednesday, 21 March 2018

What’s new in TypeScript

Microsoft has released the production version of TypeScript 2.7, as well as a beta version of TypeScript 2.8.

Where to download TypeScript

You can download TypeScript through NuGet or via NPM. To get TypeScript 2.7 via NPM, use the following command: npm install -g typescript. To get the TypeScript 2.8 beta, use npm install -g typescript@rc or via NuGet.

Next version: Features in TypeScript 2.8

The beta version of TypeScript adds a conditional types construct for modeling. Based on JavaScript’s conditional syntax, conditional types help with modeling of simple choices based on types at runtime while allowing more expressive design-time constructs. The construct takes the following form: A extends B ? C : D. It should be read as “If the type A is assignable to B, then the type boils down to C and otherwise becomes D.” Conditional types also offer a new way to infer new types from types compared against the new infer keyword, which introduces a new type variable. TypeScript 2.8 also offers new type aliases that use conditional types.
Other new features in TypeScript 2.8 include:
  • Pragma comments are supported for specifying the source of a JSX factory in a file. JSX is an embeddable XML-like syntax supported by TypeScript; it can be transformed into JavaScript.
  • Also, with JSX, the Element is resolved via the JSX factory. This capability is helpful when mixing and matching libraries such as React and Preact, or when mixing different versions of a library. At present, when JSX is used, TypeScript looks up a global JSX namespace for finding certain types. With Version 2.8, the compiler will try to look up the namespace based on the location of the JSX factory. Microsoft advises that going forward, new JSX-oriented libraries should avoid having JSX in the global scope. Instead, it should be exported from the respective factory function.
  • Granular control on mapped type modifiers enables more explicit syntax for adding and removing modifiers in mapped types with the +and  operators.

Current version: The new features in TypeScript 2.7

TypeScript 2.7 brings three noteworthy improvements: definite assignment checks for class properties, fixed length tuples, and improved narrowing for the in and instanceof operators. 
With fixed length tuples, Microsoft notes, “tuple types now encode their arity [the number of elements they include] into the the type of their respective length property, and tuples of different types are no longer assignable to each other.” This is a breaking change.
TypeScript 2.7 also is slated to add refactoring of the CommonJS moduleto ECMAScript 6, with this being the version of the standard JavaScript specification approved by ECMA International in 2015.
Other new capabilities in TypeScript 2.7 include:
  • Definite assignment checks for class properties. A new flag, --strictPropertyInitialization, performs checks to ensure that each instance property of a class is initialized in the constructor body or by a property initializer. A compiler option, this flag will be turned on with other --strict mode flags. To turn off this checking, developers can set the --strictPropertyInitialization  setting to false in tsconfig.json’s compilerOptions, or --strictPropertyInitializationfalse on the command line.
  • Improvements related to narrowing for in and instanceof operators. The in operator now acts as a narrowing expression for types, with types narrowed out that do not explicitly declare properties of a given name. The instanceof operator now leverages the inheritance chain, rather than relying on structural compatibility. This more accurately reflects how instanceof may behave at runtime.
  • Definite assignment assertions, in which a variable or property declaration can include a definite assertion in the form of a “!” character after the variable or property identifier. This assertion instructs the control flow analyzer to consider the variable definitely assigned, even if the analyzer cannot prove it.
  • Improvements to the pretty flag, for making error messages easier to read. TypeScript 2.7 adds the use of colors for file names, diagnostic codes, and line numbers. Also, file names and positions are formatted so common terminals can use Ctrl-click, Cmd-click and Alt-click to jump to the appropriate location in a developer’s editor.
  • Support for ECMAScript numeric separators, for placing separators between digits to distinguish between digit groups.

The new features in TypeScript 2.6

Released on October  2017, TypeScript 2.6 introduced a strict mode flag, which is identified as --strictFunctionTypes. In strict mode, any function type that does not originate from a method has its parameters compared “contravariantly,” said Microsoft’s Daniel Rosenwasser, program manager for TypeScript.
TypeScript traditionally compared parameters in a “bivariant” manner, allowing for benefits such as a simpler model for working with arrays. TypeScript 2.6 offers a compromise of increasing strictness on all function types except methods while letting TypeScript continue modeling use cases such as event handlers and simpler array handling.
Another key set of new features in TypeScript 2.6 covers error suppression comments, which are introduced via // @ts-ignore comments. Microsoft has avoided error suppression in TypeScript because most cases where users asked for it could be solved through more accurate declaration files, or using a type assertion to any, Rosenwasser said.
“However, over time, we have seen two motivating examples,” Rosenwasser added, “migrating from JavaScript to TypeScript, and overcoming type checks that live in legacy code.” When migrating JavaScript to TypeScript, error suppression can help with a situation in which developers run into a pattern that is difficult to model. Developers could spend time trying to understand the pattern but may want to get of it later anyway. Now they can use suppression comments to avoid errors where code will still run fine.
For overcoming type checks in legacy code, some large organizations update project dependencies in tandem. Any change that introduces a type-checking error requires a fix to avoid breaking a build. “While the error is usually useful, the reality of the situation is that the code has functioned thus far and teams have finite resources,” Rosenwasser said. Microsoft advises using suppression comments sparingly, and always with an explanation.
Also in TypeScript 2.6:
  • Tagged template strings in a module are now cached after the initial invocation. This move aligns TypeScript more closely with recent revisions to the ECMAScript specification underlying JavaScript. With this revision, developers can use libraries such as Lit-html and HyperHTML.
  • The standalone TypeScript compiler now offers localized messages over NPM when using the --locale flag.
  • The --watch mode, for emitting modules, has been made faster.
  • Tool support improvements are planned for tools such as Visual Studio and Visual Studo Code, including quick fixes for anys, with TypeScript able to infer types for declarations with types that are an implicit any.
  • Developers can refactor JSDoc documentation comments to TypeScript annotations.
  • Editors can offer a quick fix to install type declarations for untyped imports.
  • TypeScript 2.6 has some breaking changes, such as write only references being considered unused under --noUnusedLocals and --noUnusedParameters.
  • Organizational changes have been made to DOM declarations in lib.d.ts.
  • in ambient contexts, such declaration files and declare module blocks, expressions are disallowed in default exports.
  • Write-only references are now considered unused under --noUnusedLocals and --noUnusedParameters.
  • Uninhabitable types that result from intersections such as number and string (:"foo" & 42, for example) simplify to never when in a union.

What’s new in TypeScript 2.5

TypeScript 2.5, released in September 2017, includes an enhancement for try/catch statements for errors as well as compiler improvements.
The catch binding parameters capability in TypeScript 2.5 uses a late-stage ECMAScript feature to make catch binding optional in try/catchstatements. Making catch binding optional “means we can just omit unusedError altogether,” said Rosenwasser. The reason for that is there are times when developers expect something might fail by throwing an error, but the developer does not care what the error is.
Also featured in TypeScript 2.5:
  • Support for imports when using the Node module-resolution strategy. The compiler checks whether files originate from “identical” packages. If a file was derived from a package with a package.jsoncontaining name and version fields identical to a previously encountered package, TypeScript will redirect to the top-most package. This resolves a situation in which two packages might have identical declaration of classes but contain “private” members that make them structurally incompatible. A side effect of this change is reduction in memory and the runtime footprint of the compiler and language service.
  • The preserveSymlinks compiler flag, which is like to the –preserveSymlinks flag in Node.js that instructs the module loader to preserve symbolic links when resolving and caching modules.
https://www.infoworld.com/

No comments:

Post a Comment