BT

TypeScript 1.8 Beta Adds Integration with JavaScript, Stateless Functional Components and More

by Sergio De Simone on Feb 03, 2016 |

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.

Rate this Article

Relevance
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

More features for React... by Ajay Jadhav

Adding more React oriented features is a good idea, and will help people choose TypeScript for writing the React components.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss
General Feedback
Bugs
Advertising
Editorial
Marketing
InfoQ.com and all content copyright © 2006-2016 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT

We notice you’re using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.