Skip to content

Wrong pipe overload is used with React components and other generic function types #32269

@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 3.5.2

Search Terms:

Code

import * as React from 'react';

declare type AnyFunction = (...params: any[]) => any;
declare type UnknownFunction = (...params: unknown[]) => unknown;

declare function pipe<A extends unknown[], B>(
    ab: (...a: A) => B,
): (...args: A) => B;
declare function pipe<A extends unknown[], B, C>(
    ab: (...a: A) => B,
    bc: (b: B) => C,
): (...args: A) => C;
declare function pipe(...fns: AnyFunction[]): UnknownFunction;

declare const myHoc: <Props>(
    ComposedComponent: React.ComponentType<Props>,
) => React.ComponentType<Props>;

{
    const Component = (props: {}) => <div />;
    
    // unknown, expected React.ComponentType<{}>
    const result = pipe(
        () => Component,
        myHoc,
    )();
}

{
    declare class Component extends React.Component<{}> {}

    // unknown, expected React.ComponentType<{}>
    const result = pipe(
        () => Component,
        myHoc,
    )();
}

{
    declare const Component: React.ComponentType<{}>;

    // expected and actual: React.ComponentType<{}>
    const result = pipe(
        () => Component,
        myHoc,
    )();
}

If we comment out the last pipe overload, it works as expected.

The same problem applies to other generic functions. Here's the smallest test case I could find:

type ComponentType<P> = {
    (p: P): null;
    p?: P;
};

declare const myHoc: <Props>(
    ComposedComponent: ComponentType<Props>,
) => ComponentType<Props>;

{
    declare const Component: (props: {}) => null;

    // unknown, expected ComponentType<{}>
    const result = pipe(
        () => Component,
        myHoc,
    )();
}

{
    declare const Component: ComponentType<{}>;

    // expected and actual: ComponentType<{}>
    const result = pipe(
        () => Component,
        myHoc,
    )();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions