-
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
Handling Props with react and spread attributes #4682
Comments
Props
with react and spread attributes
i am not getting repro for this with latest react.d.ts from definitely typed. here are my two files /// <reference path="c:\\clones\\DefinitelyTyped\\react\\react.d.ts" />
import React = require("react");
import ChildView = require('child');
interface Props extends React.Props<View> {
foo: string;
}
interface State {
}
class View extends React.Component<Props, State> {
render() {
return <ChildView {...this.props} />;
}
}
export = View; and /// <reference path="c:\\clones\\DefinitelyTyped\\react\\react.d.ts" />
import React = require("react");
interface Props extends React.Props<View> {
foo: string;
}
interface State {
}
class View extends React.Component<Props, State> {
private removeItem = (evt: React.MouseEvent) => {
}
render() {
return <div>{this.props.foo}</div>;
}
}
export = View; |
Haa, I messed up my example, sorry, the two classes must be incompatible in some way. I edited my example: I added an incompatible |
sorry for the delay. but this seems to make sense to me, the declaration of also pinging @RyanCavanaugh |
For some reason I did not consider that the |
I'm not able to duplicate using the current files listed in the OP and the latest definitions from DT (and TS 1.6.2). I believe the DT definitions are correct here. The |
I still get the issue with TS 1.6.2 and latest (as of october 4) https://github.com/kuon/DefinitelyTyped/blob/master/react/react.d.ts You must ensure the parent and child component don't have the same method signatures for the bug to manifest. |
No idea why I wasn't able to duplicate the first time, but I was able to this time. I see the issue, but I think the typings are mostly correct for the time being and likely shouldn't be changed. Instead, for now, I'd suggest working around the issue by not extending from The issue is that in the React world there is a difference between the props that you pass in to |
Yes that's what I meant with my last paragraph (that key and ref should get a "special" handling). I tried to change the definition but I soon find out it wasn't a good idea. My current workaround is, as you said, to no extend React.Props. |
fixed; it's no longer required to extend React.Props. |
@mhegazy Can you please point me in the direction of where this is documented? Can't seem to find it. |
I think we changed the way things worked in #5478, where instead of users needing to extend |
@dsifford sorry that wasn't documented. I've filed microsoft/TypeScript-Handbook#237 on your behalf. |
@DanielRosenwasser Thanks so much! 😄 |
Given the following parent component:
And the following child component:
I get an error like this:
This is caused by the
ref
attribute of theReact.Props
having differentView
(my classes)Here is the interface defined:
I could declare props like
interface Props extends React.Props<React.Component<any, any>>
but I feel like something should be done to be able to "omit" certain attributes when using spread attributes.Something like:
Or handling
key
andref
specially in JSX and having them checked automatically, allowing the props definition to be justwhile allowing
key
andref
to be used implicitly..The text was updated successfully, but these errors were encountered: