This article is more than 1 year old

Riddle me this: TypeScript's latest data type is literally unknown

Version 3.0 improvements in Microsoft's lang also include pumped up tuples

Microsoft has rolled out version 3.0 of TypeScript, its open source extension of JavaScript that includes support for static types.

For those who love TypeScript – a growing group to judge by language rankings – or hate JavaScript – which often has something to do with its lack of static typing – that's not a bad way to start the week, apart from the breaking changes.

"TypeScript is an extension of JavaScript that aims to bring static types to modern JavaScript," said Daniel Rosenwasser, program manager on the TypeScript language at Microsoft, in a blog post. "The TypeScript compiler reads in TypeScript code, which has things like type declarations and type annotations, and emits clean readable JavaScript with those constructs transformed and removed."

That code can then be run in any ECMAScript runtime, such as a browser or Node.js, he explained.

To understand why that might be worthwhile, consider that JavaScript will let you create numerical and text variables and add them together, literally joining the two as a single string. In a strongly typed language, an error message would complain, to encourage the programmer to be specific about whether a number or string is the desired result.

A programming language that support data types helps makes type-related errors easier to catch and can reduce the amount of unit tests developers need to write to verify their code, though dealing with data types demands a bit more work up front.

TypeScript is the basis of the Angular front-end framework; the other leading front-end framework contender, React, can be used with TypeScript or with regular JavaScript. Facebook, which created React, also developed Flow, an open-source static type checker for JavaScript that serves as an alternative to TypeScript for those concerned about type checking.

TypeScript 3.0 adds a feature called "project references" that allows devs to specify how one TypeScript project depends on another, through paths added to tsconfig.json files that point to other configuration files.

Specifying such dependencies allows projects to be broken up into more manageable chunks and can accelerate builds. And there's now a --build flag to take advantage of incremental builds.

The updated TypeScript has also adapted to JavaScript's rest parameter syntax, by which a varying number of parameters can be passed to a function as arguments. Until version 3.0, TypeScript didn't have an efficient way to assign data types to this sort of operation.

Now the language treats rest parameters as generic and turns them into tuples, inferring the data types involved along the way. According to Rosenwasser, Microsoft's TypeScript team had to do some work on tuple types to make this work.

Error messages have also received some attention since last TypeScript release, making them shorter and clearer.

perl

Official: Perl the most hated programming language, say devs

READ MORE

There's a new data type, called unknown, that differs from another catch-all data type, called any. "Unlike any, unknown is assignable to almost nothing else without a type assertion," explains Rosenwasser. "You also can’t access any properties off of an unknown, nor can you call/construct them."

The unknown data type might be useful in a place where a dev wants to ensure that type checking is performed or that a type assertion is used. It also happens to represent one of the breaking changes – since unknown is now a reserved word, previous TypeScript code that used it as a variable name won't perform as expected.

Other additions to version 3.0 include broader JSX support (JSX being a JavaScript extension used in React) and a new reference directive for polyfills (libraries that make new APIs work with older runtimes).

"Going forward, we foresee bringing more value to the type system and tooling experience, polishing the existing work on project references, and making TypeScript (both the language and the project) more approachable by whatever means we can," said Rosenwasser. ®

More about

TIP US OFF

Send us news


Other stories you might like