11.09.2015 Views

Typescript Deep Dive by Basarat Ali Syed

Typescript Deep Dive by Basarat Ali Syed

Typescript Deep Dive by Basarat Ali Syed

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TypeScript <strong>Deep</strong> <strong>Dive</strong><br />

Components are type checked based on the props property of the component. This is modeled after how JSX is<br />

transformed i.e. the attributes become the props of the component.<br />

To create React components we recommend using ES6 classes. The react.d.ts file defines the<br />

React.Component class which you should extend in your own class providing your own Props and State<br />

interfaces. This is demonstrated below:<br />

interface Props {<br />

foo: string;<br />

}<br />

class MyComponent extends React.Component {<br />

render() {<br />

return {this.props.foo}<br />

}<br />

}<br />

<br />

Non React JSX<br />

TypeScript provides you with the ability to use something other than React with JSX in a type safe manner. The following<br />

lists the customizability points, but note that this is for advanced UI framework authors:<br />

You can disable react style emit <strong>by</strong> using "jsx" : "preserve" option. This means that JSX is emitted as is and then<br />

you can use your own custom transpiler to transpile the JSX portions.<br />

Using the JSX global module:<br />

You can control what HTML tags are available and how they are type checked <strong>by</strong> customizing the<br />

JSX.IntrinsicElements interface members.<br />

When using components:<br />

You can control which class must be inherited <strong>by</strong> components <strong>by</strong> customizing the default interface<br />

ElementClass extends React.Component { } declaration.<br />

You can control which property is used to type check the attributes (the default is props ) <strong>by</strong> customizing the<br />

declare module JSX { interface ElementAttributesProperty { props: {}; } } declaration.<br />

Fork me on github<br />

JSX<br />

70

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!