0

I am trying to use Typescript with react (javascript) I keep seeing examples in these two forms:

 1. React.Component<any, any>
 2. React.Component<{}, {}>

Are 1 and 2 equivalent?
Which one is the prefered style?

FOLLOWUP: I was reading this book: charleslbryant.gitbooks.io/hello-react-and-typescript/content/…. This code uses React.Component<{}, {}>. Does this mean null, null so no state or properties for this component?

thrave
  • 111
  • 1
  • 11
  • 3
    No. `{}` is an empty interface and it's not that same as `any`. E.g. `const value: {} = null;` is invalid, but `const value: any = null;` is fine. – cartant Apr 25 '18 at 20:03
  • I was reading this book: https://charleslbryant.gitbooks.io/hello-react-and-typescript/content/TypeScriptAndReact.html. This code uses React.Component<{}, {}>. Does this mean null, null so no state or properties for this component? – thrave Apr 25 '18 at 20:15
  • 1
    No, it means the component requires two **objects**. –  Apr 25 '18 at 20:19
  • 2
    Well, that page of the book has some inconsistencies: it mentions `any` in the text, but uses `{}` in the code example. With props, using `{}` is saying that the component expects no props. With the state, if you're not creating a state, then the state type is redundant; internally, the state's value would be `undefined`. I think that book might be a little out-of-date, as the state type is now optional. In fact, [both the props and state types are now optional](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/c9de295a0942129af7d4f16865326c33fb53fa34/types/react/index.d.ts#L281). – cartant Apr 25 '18 at 20:31
  • Thanks, that "any in the text, but uses {}" part of the book confused me and thus I asked this somewhat redundant question. – thrave Apr 25 '18 at 21:04

0 Answers0