RyanCavanaugh / jsx-intro

Intro to using TypeScript with JSX
Apache License 2.0
25 stars 3 forks source link

provide example that shows usage of React.createClass #4

Open HamletDRC opened 8 years ago

HamletDRC commented 8 years ago

In our codebase we cannot turn on the ES6 target, so I believe that we cannot use the ES6 class format that this repo describes.

Instead we use React.createClass.

Could you please come up with an example of how to use React.createClass and have the types for the Props validated? We are using .tsx for a few months now but we just realized that our Props are not being validated. I have been trying on my own to find a way to get the Props validated but so far have had no luck.

Thanks in advance for any help/insight you might have.

HamletDRC commented 8 years ago

I also found this blog post:

http://staxmanade.com/2015/08/playing-with-typescript-and-jsx/

Which says:

Thanks to a tip from Ryan (ya, the famous Ryan from the TypeScript team) has a great write up about TypeScript and JSX we should be avoiding all of the above use of React.createClass(...) and instead using the ES6 extends functionality which we can leverage in TypeScript.

Do you have any further explanation?

RyanCavanaugh commented 8 years ago

You don't need to target ES6 to use classes. TypeScript will convert class YourComponent extends React.Component<..., ...> { to the appropriate ES5 code that will work on an ES5 runtime with React. I don't recommend trying to use createClass with JSX and TypeScript; you'll end up doing a lot of manual type annotation you wouldn't otherwise need.

HamletDRC commented 8 years ago

We have about 500 React components that we converted to .tsx files. So the idea of converting them all to use classes that extends React.Component is a lot of work. It took us quite a long time to convert them to .tsx in the first place! But we did not realize that we would lose the type safety.

So far I have not been able to make an example which shows React.createClass and type safety. Is it possible somehow with the correct type annotations?