Skip to content

Instantly share code, notes, and snippets.

@osamaqarem
Last active May 17, 2026 18:47
Show Gist options
  • Select an option

  • Save osamaqarem/77ec84e4cca61cac1cdb84f0c8d75d03 to your computer and use it in GitHub Desktop.

Select an option

Save osamaqarem/77ec84e4cca61cac1cdb84f0c8d75d03 to your computer and use it in GitHub Desktop.
`as` polymorphism in React 19
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