score:1

i would probably declare superpropstype.children as:

children: react.reactelement<subpropstype1> | react.reactelement<subpropstype1>[];

to account for the possibility of having both a single and multiple children.

in any case, as pointed out already, that won't work as expected.

what you could do instead is declare a prop, let's say subcomponentprops: subpropstype1[], to pass down the props you need to create those subcomponent1s, rather than their jsx, and render them inside supercomponent:

interface superpropstype {
  children?: never;
  subcomponentprops?: subpropstype1[];
}

...

const supercomponent: react.fc<superpropstype> = ({ subcomponentprops }) => {
  return (
    ...

    { subcomponentprops.map(props => <subcomponent1 key={ ... } { ...props } />) }

    ...
  );
};

score:12

as of typescript 3.1, all jsx elements are hard-coded to have the jsx.element type, so there's no way to accept certain jsx elements and not others. if you wanted that kind of checking, you would have to give up the jsx syntax, define your own element factory function that wraps react.createelement but returns different element types for different component types, and write calls to that factory function manually.

there is an open suggestion, which might be implemented as soon as typescript 3.2 (to be released in late november 2018), for typescript to assign types to jsx elements based on the actual return type of the factory function for the given component type. if that gets implemented, you'll be able to define your own factory function that wraps react.createelement and specify it with the jsxfactory compiler option, and you'll get the additional type information. or maybe @types/react will even change so that react.createelement provides richer type information, if that can be done without harmful consequences to projects that don't care about the functionality; we'll have to wait and see.


Related Query

More Query from same tag