Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference not working for React ref callback param #7088

Closed
use-strict opened this issue Feb 15, 2016 · 3 comments
Closed

Type inference not working for React ref callback param #7088

use-strict opened this issue Feb 15, 2016 · 3 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@use-strict
Copy link

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 component
render() {
    return <Overlay ref={ref => this.overlayEl = ReactDOM.findDOMNode(ref) as HTMLElement} />;
    // 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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Feb 16, 2016
@RyanCavanaugh
Copy link
Member

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.

@RyanCavanaugh RyanCavanaugh added Needs More Info The issue still hasn't been fully clarified and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Feb 16, 2016
@RyanCavanaugh RyanCavanaugh self-assigned this Feb 16, 2016
@RyanCavanaugh
Copy link
Member

... OK it turns out we already do that. I'll investigate what's going on.

@mhegazy mhegazy added the Bug A bug in TypeScript label Feb 20, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Feb 20, 2016
@RyanCavanaugh RyanCavanaugh removed the Needs More Info The issue still hasn't been fully clarified label Mar 1, 2016
@yuit yuit assigned yuit and unassigned RyanCavanaugh Jun 3, 2016
@yuit
Copy link
Contributor

yuit commented Jun 13, 2016

already fix in master and latest nightly built Here is the scenario I tried using typing files from @types
ref parameter of an error function is now inferred to be Overlay

import * as React from "react"
import * as ReactDOM from "react-dom"

class Overlay extends React.Component<{}, {}> {
    render() {
        return <div> Overlay </div>
    }
}
class C extends React.Component<{}, {}> {
    overlayEl: HTMLElement;
    render() {
        return <Overlay ref={ref => this.overlayEl = ReactDOM.findDOMNode(ref) as HTMLElement} />;
    }
}
{
    "compilerOptions": {
        "noImplicitAny": true,
        "jsx": "react"
    }
}

@yuit yuit closed this as completed Jun 13, 2016
@yuit yuit added the Fixed A PR has been merged for this issue label Jun 13, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants