Created
November 16, 2022 04:46
-
-
Save crutchcorn/3779e476d3afd119a59f2c940faada32 to your computer and use it in GitHub Desktop.
A TypeScipt type to add children to older React components that don't have children declared
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ComponentType, PropsWithChildren } from "react"; | |
import { Text } from "react-native"; | |
import { Wrapper } from "aws-amplify-react-native"; | |
// 🪄 This is the magic | |
type AddChildrenToComponent<T> = T extends ComponentType<infer P> | |
? ComponentType<PropsWithChildren<P>> | |
: never; | |
const WrapperWithChildren = Wrapper as AddChildrenToComponent<typeof Wrapper>; | |
export const YourComp = () => { | |
return ( | |
<WrapperWithChildren> | |
<Text>Hello, world!</Text> | |
</WrapperWithChildren> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment