-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Props requiring casting on complex type (and defaultProps not settable) #28614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The error for
The error for the props casing (i.e. fails on
|
The strange part of the first error is that TDefaultElement is assignable to... that big list of possible values, but it seems the compiler doesn't know that. |
For future me: Here's a non-react minimal repro for the type ForwardRefRenderFunction<T, P> = (props: P) => T;
type ForwardRefExoticComponent<P> = { defaultProps?: Partial<P> };
declare function forwardRef<T, P = {}>(render: ForwardRefRenderFunction<T, P>): ForwardRefExoticComponent<PropsWithoutRef<P>>;
type PropsWithoutRef<P> =
'ref' extends keyof P
? Pick<P, Exclude<keyof P, 'ref'>>
: P;
function renderAsComponent<
TOwnProps,
TDefaultElement
>(
factory: (x: TOwnProps & {renderAs?: unknown}) => TDefaultElement,
defaultElement: TDefaultElement,
) {
const forward = forwardRef(factory);
forward.defaultProps = { renderAs: defaultElement };
} (the literal type on tags bit has been fixed since this was opened). The underlying cause twofold.
For my future self/others, I have a branch with a potential direction for the first bit; but it's not really useful to fix this without the second bit already in. |
Search Terms:
defaultProps React.RefForwardingComponent ExoticComponent
Code
Expected behavior:
defaultProps
can be set directlyprops
work without castingActual behavior:
defaultProps
cannot be set directly (see workaround in code)props
do not work without being cast.Playground Link:
Related Issues:
No.
The text was updated successfully, but these errors were encountered: