GithubHelp home page GithubHelp logo

Comments (5)

sophiebits avatar sophiebits commented on May 3, 2024

@eps1lon Remind me if this is a limitation of core TypeScript?

from react.

Youngemmy5956 avatar Youngemmy5956 commented on May 3, 2024

import React, { ReactElement, ReactNode } from "react";

type ValidChild = ReactElement<{ customProp: string }>;

type ParentProps = {
children: ValidChild | ValidChild[];
};

const Parent: React.FC = ({ children }) => {
if (!Array.isArray(children)) {
children = [children];
}

const invalidChildren = children.filter(
(child) => child.type !== ChildComponent
);

if (invalidChildren.length > 0) {
throw new Error("Invalid child components in Parent");
}

return

{children}
;
};

type ChildProps = {
customProp: string;
};

const ChildComponent: React.FC = ({ customProp }) => {
return

{customProp}
;
};

export default function App() {
return (


Parent should complain



);
}

from react.

speculees avatar speculees commented on May 3, 2024

import React, { ReactElement, ReactNode } from "react";

type ValidChild = ReactElement<{ customProp: string }>;

type ParentProps = { children: ValidChild | ValidChild[]; };

const Parent: React.FC = ({ children }) => { if (!Array.isArray(children)) { children = [children]; }

const invalidChildren = children.filter( (child) => child.type !== ChildComponent );

if (invalidChildren.length > 0) { throw new Error("Invalid child components in Parent"); }

return

{children}
;
};
type ChildProps = { customProp: string; };

const ChildComponent: React.FC = ({ customProp }) => { return

{customProp}
;
};
export default function App() { return (

Parent should complain

);
}

I was thinking about this approach, but this is would loop (filter) children in runtime instead of using typescript.

from react.

Youngemmy5956 avatar Youngemmy5956 commented on May 3, 2024

What about using this approach ??

This approach allows you to catch errors at compile time and ensures that the child components adhere to the expected props and structure.

import React, { ReactElement, ReactNode } from "react";

type ChildProps = {
customProp: string;
};

type ChildComponentProps = {
children: ReactNode;
customProp: string;
};

const ChildComponent: React.FC = ({ customProp }) => {
return

{customProp}
;
};

const Parent: React.FC = ({ children, customProp }) => {
return (


{customProp}

{children}

);
};

export default function App() {
return (




);
}

from react.

eps1lon avatar eps1lon commented on May 3, 2024

It's a TypeScript limitation because <ValidChild /> will be any JSX element i.e. (< ValidChild />).type any not ValidChild. It would only work if you'd use a JSX factory directly i.e. createElement(ValidChild). In that case, TypeScript would recognize the real element type (createElement(ValidChild).type is ValidChild) and your parent could safely assume children: ValidChild is enforced at the type level.

But this shifts the burden to component consumers which defeats the purpose.

Closing in favor of microsoft/TypeScript#14729

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.