Last active
May 17, 2026 18:47
-
-
Save osamaqarem/77ec84e4cca61cac1cdb84f0c8d75d03 to your computer and use it in GitHub Desktop.
`as` polymorphism in React 19
This file contains hidden or 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 React, { JSX } from 'react' | |
| type IntrinsicElements = keyof JSX.IntrinsicElements | |
| // Intrinsic elements e.g. 'div' | |
| type IntrinsicElementProps<Element extends IntrinsicElements> = { | |
| as?: Element | |
| } & React.ComponentPropsWithRef<Element> | |
| // Custom React components e.g. next/link | |
| type CustomElementProps<Element extends React.ElementType> = { | |
| as?: any | |
| ref?: React.Ref<React.ComponentRef<Element>> | |
| } & React.ComponentPropsWithoutRef<Element> | |
| export type PolyProps<Element, CustomProps> = Element extends IntrinsicElements | |
| ? IntrinsicElementProps<Element> & CustomProps | |
| : Element extends React.ElementType | |
| ? CustomElementProps<Element> & CustomProps | |
| : never |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment