So I feel like I'm missing something obvious. I've read the documentation on JSX in non-React contexts and I I don't see what I'm doing wrong. Consider the following code:
/** @jsx pragma */
function pragma(node: any, props: any, ...children: any[]) {
return node;
}
declare namespace JSX {
type Element = string;
interface ElementChildrenAttribute {
children: {}; // specify children name to use
}
}
interface PropsType {
children: string;
name: string;
}
export const Prop = (props: PropsType) => {};
const foo = <Prop name="foo">hello</Prop>;
The error I get (on the last line) is:
Property 'children' is missing in type '{ name: string; }' but required in type 'PropsType'.
I can "correct" this with:
const foo = <Prop name="foo" children="bye">hello</Prop>;
But I expected that the hello would be assigned as the children. I shouldn't be specifying an explicit children property, right? What am I missing here? I would expect the original to be fine (not generate an error) because the children field should be filled in with the string value "hello" in that case (and not require me to add a children prop?!?).
What am I missing here? Is there some other type/field of the JSX namespace I need to fill in here to make this work?
Clarification: My concern here is that TypeScript isn’t assigning the string “hello” to children. If it did that everything would be fine. As such, it doesn’t seem to be an issue with my prop types so much as I’m missing something in my redeclaration of the JSX namespace that tells the compiler how to handle my “custom” JSX.
Also, it is true I'm not required to have children in my props. But I'm certainly allowed to and I definitely want to because this allows me to narrow the type. This is definitely allowed (see second paragraph here) for precisely this reason. Adding it here does not create a new prop, it simply refines whatever was defined in JSX.ElementChildrenAttribute.