Skip to content
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

MockedProvider has incorrect typescript signature #7825

Closed
espretto opened this issue Mar 11, 2021 · 3 comments · Fixed by #7833
Closed

MockedProvider has incorrect typescript signature #7825

espretto opened this issue Mar 11, 2021 · 3 comments · Fixed by #7833

Comments

@espretto
Copy link

espretto commented Mar 11, 2021

By extending React.Component the MockedProvider already has a typed property children of type React.ReactNode. The interface MockedProviderProps incorrectly overrides it w/ React.ReactElement.

The error occurs for example when trying to preset the provider. This is how to re-produce the error:

import { MockedProvider } from "@apollo/client/testing";

const PresetProvider: React.FC = ({ children }) => (
  <MockedProvider addTypename={false} mocks={[]}>
    {children}
  </MockedProvider>
);

Typescript language-server error message:

var children: React.ReactNode
Type 'ReactNode' is not assignable to type '(ReactElement<any, string | JSXElementConstructor<any>> & (boolean | ReactChild | ReactFragment | ReactPortal | null)) | undefined'.
  Type 'null' is not assignable to type '(ReactElement<any, string | JSXElementConstructor<any>> & (boolean | ReactChild | ReactFragment | ReactPortal | null)) | undefined'.ts(2322)
MockedProvider.d.ts(14, 5): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<MockedProvider> & Pick<Readonly<MockedProviderProps<{}>> & Readonly<...>, never> & Partial<...> & Partial<...>'

The exact code-line is

@benjamn
Copy link
Member

benjamn commented Mar 11, 2021

@espretto Thanks for noticing this and reporting it! We'll take a look shortly.

@kevinperaza
Copy link
Contributor

The type is being overridden on purpose to make React.cloneElement happy,

{React.cloneElement(React.Children.only(children), { ...childProps })}

DefinitelyTyped

   // DOM Elements
    // ReactHTMLElement
    function cloneElement<P extends HTMLAttributes<T>, T extends HTMLElement>(
        element: DetailedReactHTMLElement<P, T>,
        props?: P,
        ...children: ReactNode[]): DetailedReactHTMLElement<P, T>;
    // ReactHTMLElement, less specific
    function cloneElement<P extends HTMLAttributes<T>, T extends HTMLElement>(
        element: ReactHTMLElement<T>,
        props?: P,
        ...children: ReactNode[]): ReactHTMLElement<T>;
    // SVGElement
    function cloneElement<P extends SVGAttributes<T>, T extends SVGElement>(
        element: ReactSVGElement,
        props?: P,
        ...children: ReactNode[]): ReactSVGElement;
    // DOM Element (has to be the last, because type checking stops at first overload that fits)
    function cloneElement<P extends DOMAttributes<T>, T extends Element>(
        element: DOMElement<P, T>,
        props?: DOMAttributes<T> & P,
        ...children: ReactNode[]): DOMElement<P, T>;

    // Custom components
    function cloneElement<P>(
        element: FunctionComponentElement<P>,
        props?: Partial<P> & Attributes,
        ...children: ReactNode[]): FunctionComponentElement<P>;
    function cloneElement<P, T extends Component<P, ComponentState>>(
        element: CElement<P, T>,
        props?: Partial<P> & ClassAttributes<T>,
        ...children: ReactNode[]): CElement<P, T>;
    function cloneElement<P>(
        element: ReactElement<P>,
        props?: Partial<P> & Attributes,
        ...children: ReactNode[]): ReactElement<P>;

This should work appropriately: kevinperaza#2

@jcreighton
Copy link
Contributor

Thanks @kevinperaza! It looks the PR wasn't opened up against the correct base. Could you please change that? Then I can get it merged.

@brainkim brainkim assigned brainkim and unassigned brainkim Jun 22, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants