-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
React: Type inference not working for ref callback #16019
Comments
The issue here is that the type of if this component does not have any properties, then declare it as if the intention is to allow any property to be defined on this component, then declare at as |
Thank you, @mhegazy! In my concrete case I actually did have a concrete Props interface defined, I just didn't think about this when trying to come up with a minimal example (I'm still learning the nuances of the type system). Nonetheless, I'm simply unable to properly type the ref callbacks, especially when defining Props interfaces that extend other, generic props (e.g. It's most likely a misunderstanding on my side, still learning ts and so I'll come up with a more concrete example today or tomorrow. |
this works for me. if you have a sample we can look more. interface ContainerProps {
someProp: number;
}
class Container extends React.Component<ContainerProps, any> {
render() {
return <div ref={p => 0} />; // p is HTMLDivElement
}
}
var x = <Container someProp={3} ref={p => 0} /> // p is Container |
Thank you, your example indeed works. Finally I had time to come up with a better example that doesn't work for me. I'm probably doing something wrong I think: interface MyInputProps extends React.ChangeTargetHTMLProps<HTMLInputElement> {
onValid?(): boolean;
onInvalid?(): boolean;
}
class MyInput extends React.Component<MyInputProps, any> {
render() {
const { onValid, onInvalid, ...inputProps } = this.props;
return (
<input { ...inputProps } />
);
}
}
var x = <MyInput ref={p => 0} /> // p is any When I try to explicitly set a type like
When I try to set the type to
Clearly I have no idea what's going on here and I'm just randomly throwing types at my code until the compiler stops complaining 😆 |
I'm so sorry for spamming your inbox :) I think I finally figured out why TS is complaining (somehow I always missed the When I change the props interface to this: interface MyInputProps extends React.ChangeTargetHTMLProps<MyInput & HTMLInputElement> {} (note the |
type Ref<T> = string | ((instance: T) => any);
interface ClassAttributes<T> extends Attributes {
ref?: Ref<T>;
} The result here is that there is no contextual type for the function, and this declare var x: string | ((a: HTMLInputElement) =>void);
x = a => { }; |
Awesome, thank you! |
Update: The compiler did instantiate I think the issues here are that:
|
Hi @yuit, I'm sorry, but somehow I got never notified about the edit and mention in your comment. The reason I'm doing |
@simonvizzini no problem at all. Just to understand it better, are you plan to pass interface MyInputProps extends React.ChangeTargetHTMLProps<HTMLInputElement> {
onValid?(): boolean;
onInvalid?(): boolean;
}
class MyInput extends React.Component<MyInputProps, any> {
render() {
const { onValid, onInvalid, ...inputProps } = this.props;
return (
<input { ...inputProps } />
);
}
}
function someFunction(...) {...}
var x = <MyInput ref={p => 0} onChange={someFunction} /> // p is any |
Hi @yuit, sorry once more for the late reply- I got kind of lost in other tasks and I didn't keep track of this issue. To answer your question, the intention is to intercept the interface MyInputProps extends React.ChangeTargetHTMLProps<HTMLInputElement> {
onValid?(): boolean;
onInvalid?(): boolean;
}
class MyInput extends React.Component<MyInputProps, any> {
render() {
const { onValid, onInvalid, onChange, ...inputProps } = this.props;
return (
<input
{ ...inputProps }
onChange={ this._onChange }
/>
);
}
private _onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
// do some validation first...
// ... then trigger onChange callback, if present:
if (this.props.onChange) {
this.props.onChange(event);
}
}
}
function someFunction(...) {...}
var x = <MyInput ref={p => 0} onChange={someFunction} /> Hope this makes sense. Let me know if you have any other questions. |
would love to see this get fixed! |
i am also facing similar type of issue. ERROR
|
The reported case is about the parameter not being contextually typed for this |
Type inference is not working for the
ref
callback prop.I believe this is the same issue as in #7088, which was fixed over a year ago.
TypeScript Version: 2.3.2
Code
Expected behavior:
Type of parameter
ref
should be inferred asTest
Actual behavior:
Parameter 'ref' implicitly has an 'any' type.
The text was updated successfully, but these errors were encountered: