Skip to content

Loss of function parameter typing when inferring props of React componentΒ #47185

@quantizor

Description

@quantizor

Bug Report

πŸ”Ž Search Terms

polymorphic component, react component prop inference

πŸ•— Version & Regression Information

I noticed this 4.5.2, not sure how far the issue goes back or if it ever worked.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

import React from 'react'

type ComponentOrHTMLTag = keyof JSX.IntrinsicElements | React.ComponentType<any>;

type CustomComponentProps<
  ActualComponent extends ComponentOrHTMLTag,
  ActualComponentProps = React.ComponentProps<ActualComponent>
> = ActualComponentProps & {
    component?: ActualComponent;
  };

interface CustomComponent<
  FallbackComponent extends ComponentOrHTMLTag,
> {
  <ActualComponent extends ComponentOrHTMLTag = FallbackComponent>(
    props: CustomComponentProps<ActualComponent>
  ): React.ReactElement<CustomComponentProps<ActualComponent>>;
}

const Field = ({component: Component = 'input', ...props}: {component: ComponentOrHTMLTag}) => <Component {...props} />

export const DSField: CustomComponent<'input'> = Field;

type Item = {
  name: string
}

const ToyInputWithFunctionProp = ({ exampleFunction, item, ...props }: { exampleFunction: (item: Item) => string,  item: Item}) => <input data-thing={exampleFunction(item)} {...props} />

function main() {
  return (
    <DSField component={ToyInputWithFunctionProp} exampleFunction={item => item.name} item={{ name: 'foo'}} />
  )
}

πŸ™ Actual behavior

Error: Parameter 'item' implicitly has an 'any' type.

πŸ™‚ Expected behavior

item should retain its type Item

Tf you hover on exampleFunction to get the intellisense for it, the correct type is observed. It seems to be lost on the interpolated JSX value.

Screen Shot 2021-12-17 at 10 21 20 PM

Screen Shot 2021-12-17 at 10 21 31 PM

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions