TypeScript 2.6 introduces 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,” Microsoft’s Daniel Rosenwasser, program manager for TypeScript, said.
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 allowing TypeScript to continue modeling use cases including event handlers and simpler array handling.
Other new features in TypeScript 2.6 include error suppression comments, which are introduced via
// @ts-ignore comments. Microsoft has avoided error suppression within 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.
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.
Several “minor” breaking changes are included in TypeScript 2.6, according to Microsoft. Various changes have been made to DOM declarations in lib.d.ts, and write-only references are now considered unused under
—noUnusedParameters. Also, uninhabitable types that result from intersections such as number and string (
”foo” & 42, for example) simplify to “never” when in a union.