Preact: Typing the props of a component that wraps a native component

Viewed 126

With React you are able to use React.ComponentPropsWithoutRef<"img"> to elegantly type a component that passes through props to a native element.

How are you supposed to do this with Preact?

1 Answers

There is exactly no utility type similar to ComponentPropsWithoutRef provided in React typings. But you can make use of IntrinsicElements interface defined in JSXInternal namespace.

import { JSXInternal } from 'preact/src/jsx';

type ImgAttrs = JSXInternal.IntrinsicElements['img'];

let x = {} as ImgAttrs;

This works with Preact version 10.x.x.

Related