Hacker News new | past | comments | ask | show | jobs | submit login

You can totally use typescript with React, in fact it's a game changer. It's actually way more beneficial than with angular, as you have properly typed components all the way down. No crappy string templates that are not checked by typescript.



True story.

But how did you get rid of the implicit anys when using JSX?

I used this compiler flag that forbids them and suddnendly nothing worked anymore. It seemed that almost everything returned any per default.


That compiler flag is wonderful, and so is the newer 'strictNullChecks'

I use these typings: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/mast...

Then create my components like this:

interface Props {

}

interface State {

}

class MyComponent extends React.Component<Props, State> {

}

and everything is typed correctly (beside setState which is impossible to type exactly as of today)


How would you do something like this with stateless components? (Where the component is just a function that takes props)


interface IMyComponentProps { x: number; }

function MyComponent(props: IMyComponentProps): JSX.Element { return <p>{props.x}</p>; }


Maybe it was the type definition I used...


Stateless component:

  interface Props {
    bla: string
    ble: number
  }

  export default function MyComponent({ bla, ble }: Props) {

  }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: