Flow: Missing React.forwardRef definition

Created on 5 Apr 2018  ·  7Comments  ·  Source: facebook/flow

React 16.3 introduced a new forwardRef() API, for which Flow does not currently have a definition.

Most helpful comment

Any idea of which release this will be part of? Thanks

All 7 comments

I might try to work on this over the weekend.

Looks like for maximum correctness we need a new opaque type React$RefForwardingComponent<Props> (name?) and to add it as an option to the unions React$ComponentType<Props> and React$ElementType. However, and I'm extrapolating from limited experimentation here, it looks like Flow will not typecheck these props (when used in JSX etc) unless we add special handling for RefForwardingComponent (in react_kit.ml probably?)

And then there's the option of fudging it and pretending forwardRef<Props>() returns React$ComponentType<Props>, but that's obviously unsound 🤷‍♂️

OK, having done some digging, I'm going to make a PR with the less-correct solution, typing forwardRef as returning ComponentType<Props>. I'm basing this on the existing libdef for createContext: It models Provider and Consumer as ComponentType<Props> even though internally they're plain objects distinguished by their $$typeof property, and therefore are neither callable nor are they classes extending React.Component.

Ideally we would model these React builtins using a first-class "opaque component" type so we could catch certain errors that the current model will happily allow (_and_ consider to have full type coverage).

Any idea of which release this will be part of? Thanks

@tiarebalbi 0.89

I will follow up with a blog post on how to use the new AbstractComponent type. It will coincide with the 0.89 release.

@jbrown215 Can we get any idea of timelines for release?

At the latest, December 19th, but hopefully December 12th. Call these dates tentative, since unexpected blockers might pop up.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebmck picture sebmck  ·  113Comments

xtinec picture xtinec  ·  65Comments

cletusw picture cletusw  ·  52Comments

jamesisaac picture jamesisaac  ·  44Comments

jlongster picture jlongster  ·  55Comments