TypeScript, Microsoft’s open source superset of JavaScript, has reached 1.8 beta, bringing many new language features and tools enhancements.
Possibly the most impactful feature of TypeScript 1.8 is its compiler being now able to consume JavaScript alongside TypeScript files. This feature, enabled by the --allowJS
command line flag, will make it seamless to use JavaScript code within a TypeScript project, without forcing developers to convert all of their JavaScript files at once, a task that may yield compilation errors and that can be now carried through at the pace that developers prefer. Besides that, the possibility of compiling JavaScript code means that any 3rd party libraries can be included in a TypeScript compilation, so no more external packaging is required to bundle them together.
Stateless functional components, an idea originated within the React project, have also found their way into TypeScript 1.8 beta. Their main appeal lies with their being lightweight components that do no have state and can be easily composed. The following is an example of a stateless functional component in TypeScript:
let SimpleGreeter = ({name = ‘world‘}) => <div>Hello, {name}</div>;
That is equivalent to the following:
interface GreeterProps {
name: string;
}
class Greeter extends React.Component<GreeterProps, {}> {
render() {
return <div>Hello, {this.props.name}</div>;
}
}
Stateless functional components require the latest version of react.d.ts.
TypeScript 1.8 beta also adds support for F-bounded polymorphism, which comes down to the possibility for a type parameter constraint to reference type parameters from the same type parameter list. The following code snippet, where you can see that the type T
is constrained by another type in the same parameter list, U
, is now legal in TypeScript:
function assign<T extends U, U>(target: T, source: U): T {
for (let id in source) {
target[id] = source[id];
}
return target;
}
Another welcome enhancement to the TypeScript compiler is control flow analysis, which will help catch common error such as unreachable code, unused labels, implicit returns, and others.
TypeScript 1.8 beta includes many more new features, such as simplified props
type management in React, string literal types, improved type inference for union/intersection types, etc. You can find out more in the official What’s New.
As mentioned, TypeScript is a strict superset of JavaScript, to which it adds optional static typing and support for class-based object-oriented programming. TypeScript 1.8 beta is available for Visual Studio 2015, NuGet, and npm.