Paweł Krystkiewicz
All articles

2 min read

TS/React #01: Where are my prop types?!

You got yourself a nice third-party component, but there's a catch: it doesn't export its prop types. What now?

The Type Extraction Problem

When working with third-party libraries, you often want to pass props from your component down to a library component. But what if the library doesn't export its prop types? Say you are working with Framer Motion and you want to pass props to the motion.div component. Sometimes the type exists under a name you would not guess (here it is HTMLMotionProps<'div'>). Sometimes the library exports nothing at all.

tsx
// You want to do this:
interface MyComponentProps {
  animationProps?: MotionDivProps // No such type. You'd have to know it is called HTMLMotionProps<'div'>
}

Either the type hides behind a name you can't guess, or it isn't exported at all (on purpose, or by mistake in a release). ComponentProps works in both cases, and you don't need to know which one you got.

The naive solution? Copy-paste the types from the library's source code or the docs. But this is brittle, error-prone, and breaks when the library updates. How to do it like a pro?

Solution

React.ComponentProps<T> extracts component props. It ships with React's type definitions (@types/react), not with TypeScript itself. Usage looks like this:

tsx
interface MyComponentProps {
  animationProps?: React.ComponentProps<typeof motion.div>
}

That's it. Clean, type-safe, and automatically stays in sync with the library. You can use it with any component you want.

How It Works

The magic happens in two steps:

  1. typeof motion.div gets the actual type of the component (not an instance, but the component itself)
  2. React.ComponentProps<...> extracts all props that component accepts

This pattern works with any React component:

tsx
import { motion } from 'framer-motion'
import { Button } from '@some-ui-library'

interface MyProps {
  // Extract props from motion.div
  motionProps?: React.ComponentProps<typeof motion.div>

  // Extract props from any component
  libraryButtonProps?: React.ComponentProps<typeof Button>

  // Even works with intrinsic HTML elements. This is the complete prop type:
  // it has type, disabled and ref
  buttonProps?: React.ComponentProps<'button'>
  // Only the attributes shared by all elements: it rejects type and disabled
  // (an anchor's href too) and has no ref
  commonProps?: React.HTMLAttributes<HTMLButtonElement>
}

Practical Usage

Here's a real-world example with Framer Motion:

tsx
import { motion } from 'framer-motion'

interface AnimatedCardProps {
  title: string
  children: React.ReactNode
  animationProps?: React.ComponentProps<typeof motion.div>
}

export const AnimatedCard = ({
  title,
  children,
  animationProps,
}: AnimatedCardProps) => {
  return (
    <motion.div
      initial={{ opacity: 0, y: 20 }}
      animate={{ opacity: 1, y: 0 }}
      {...animationProps}
    >
      <h2>{title}</h2>
      {children}
    </motion.div>
  )
}

// Usage - full type safety!
<AnimatedCard
  title='Hello'
  animationProps={{
    transition: { duration: 0.5 },
    whileHover: { scale: 1.05 },
    exit: { opacity: 0 }, // TypeScript knows all valid motion.div props
  }}
>
  Content here
</AnimatedCard>

Edge Cases to Consider

Generic Components: If the component is generic, you might need to provide type arguments. typeof Select<string> is an instantiation expression, so it needs TypeScript 4.7 or later:

tsx
interface Props {
  selectProps?: React.ComponentProps<typeof Select<string>>
}

Ref forwarding: ComponentProps includes ref. On React 19 ref is a regular prop, so that is what you want. On React 18 and older a plain function component never receives ref (only forwardRef components do). For props you spread into your own function component, the honest type there is ComponentPropsWithoutRef:

tsx
// React 19: includes the ref prop
type MotionDivProps = React.ComponentProps<typeof motion.div>

// React 18 and older: no ref
type MotionDivPropsNoRef = React.ComponentPropsWithoutRef<typeof motion.div>

// The explicit opposite: always includes ref
type MotionDivPropsWithRef = React.ComponentPropsWithRef<typeof motion.div>

Excluding specific props: Sometimes you want to exclude certain props:

tsx
interface Props {
  animationProps?: Omit<
    React.ComponentProps<typeof motion.div>,
    'children' | 'ref'
  >
}

Going Forward

To make this pattern even more powerful, consider:

  • All props optional: Use Partial<React.ComponentProps<...>>
  • Required props: Use Required<Pick<...>> to enforce specific props
  • Discriminated unions: Combine with unions for conditional prop spreading
  • Generic wrappers: Create reusable type helpers for common patterns
tsx
type PartialProps<T extends React.ElementType> = Partial<React.ComponentProps<T>>
type RequiredProps<
  T extends React.ElementType,
  K extends keyof React.ComponentProps<T>
> = Required<Pick<React.ComponentProps<T>, K>>

Conclusion

The React.ComponentProps utility type solves a common TypeScript challenge elegantly. By extracting props from components directly, we can:

  • Maintain type safety without manual type definitions
  • Stay automatically in sync with library updates
  • Reduce boilerplate and maintenance burden
  • Make our component APIs more flexible and predictable

Next time you're reaching for a third-party component's props, remember: you don't need the library to export types. @types/react already has your back.