jsx object invalid data attribute nested component react
We have this behaviour for JSX:
Note: If an attribute name is not a valid JS identifier (like a data-* attribute), it is not considered to be an error if it is not found in the element attributes type.
https://www.typescriptlang.org/docs/handbook/jsx.html#attribute-type-checking
import React, { FC } from 'react';
const MyComponent: FC<{ myProp: string }> = props => null;
<MyComponent myProp="foo" data-test="foo" />;
I want this same behaviour but for object types. This is necessary for times when we want to pass some props as a nested object (for whatever reason). Currently this errors:
const MyComponent2: FC<{ props: { myProp: string } }> = props => null;
<MyComponent2 props={{ myProp: 'foo', 'data-test': 'foo' }} />;
As far as I'm aware, there's currently no way to extend the above props type to allow for this behaviour.
My suggestion meets these guidelines:
You want a combination of #6579 (#21044) and #26797. Today, the logic is hardcoded into how we check jsx literals, but with those you could define, eg, type Props<P> = P & {[index: /^data-/]: unknown}
Most helpful comment
You want a combination of #6579 (#21044) and #26797. Today, the logic is hardcoded into how we check jsx literals, but with those you could define, eg,
type Props<P> = P & {[index: /^data-/]: unknown}