You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type inference not working for React ref callback param #7088
This looks like a type inference issue with tsc 1.8.0+ (also nightly), but I'm not sure. It could also be because of typings.
The offending code looks something like this:
// Inside React componentrender(){return<Overlayref={ref=>this.overlayEl=ReactDOM.findDOMNode(ref)asHTMLElement}/>;// error TS7006: Parameter 'ref' implicitly has an 'any' type.}
I'm using ref "callback" syntax because the string-based syntax is deprecated by react and also not type safe. Needless to say, I have "noImplicitAny" enabled.
It looks like ref type is now an intersection between two types (relates to #5478), and the type inference no longer works, where previously it would be an instance of Overlay.
I can work around this issue for now by manually passing the type, like (ref: Overlay) =>, but it should work without it as well.
FWIW they say it's "legacy", not "deprecated", though in terms of their engineering cycle I have no idea what that means.
If you don't mind diverging your react.d.ts file, I'd recommend changing the definition of ref by removing the string | part of it.
The language-side fix here is non-React-specific, though -- when a function expression is contextually typed by a union type where exactly one of the constituents of the union has a call signature, we should use that type to contextually type the expression rather than giving up.
already fix in master and latest nightly built Here is the scenario I tried using typing files from Types (@types) ref parameter of an error function is now inferred to be Overlay
Activity
RyanCavanaugh commentedon Feb 16, 2016
FWIW they say it's "legacy", not "deprecated", though in terms of their engineering cycle I have no idea what that means.
If you don't mind diverging your react.d.ts file, I'd recommend changing the definition of
refby removing thestring |part of it.The language-side fix here is non-React-specific, though -- when a function expression is contextually typed by a union type where exactly one of the constituents of the union has a call signature, we should use that type to contextually type the expression rather than giving up.
RyanCavanaugh commentedon Feb 16, 2016
... OK it turns out we already do that. I'll investigate what's going on.
yuit commentedon Jun 13, 2016
already fix in master and latest nightly built Here is the scenario I tried using typing files from Types (@types)
refparameter of an error function is now inferred to beOverlay{ "compilerOptions": { "noImplicitAny": true, "jsx": "react" } }