[jsx-key] False positive when spreading props
Receiving false positive on line 5 below. Since the key is part of the props spread in the element creation, it should not be flagged.
const elements = source.map((s, index) => {
const props = {
key: index,
};
return <div {...props}></div>;
});
Most of the time an object is spread, it’s not locally contained, and thus not statically analyzeable. Why aren’t you specifying the props explicitly inline here?
This is just a simplified repro, but I'm wondering if we should err on the false negative side here since we can't properly analyze it?
I think that in the very few cases where it's justified to dynamically spread props like this, an override comment would make sense.
It might help if you provided an unsimplified repro?
Yeah agree it's an edge case and we can override. In my project, we defined a type in another file which contained all the props including key. This is probably going to be rare though, so if there's a technical challenge here with static analysis, I am okay to close this.
It might be possible to leverage typescript-eslint information to confirm there's a key present in the type, but if not, there'd be nothing we could do.
In general, I think key shouldn't be defined in any shared type like that, since it's specific to the usage of a given element, and not to all uses of the component.
Just want to throw in that we're encountering the same issue in 1 or 2 places. Happy to override with a disable directive for now as it's an edge case/uncommon occurrence for us too.