Skip to content

Generic argument inference no longer working for specific case #39080

Description

TypeScript Version: >3.9.2

Search Terms: generic argument inference TS2345

Code

declare namespace React {
    type WeakValidationMap<T> = {
        [K in keyof T]?: null extends T[K] ? string : string
    };

    interface FunctionComponent<P = {}> {
        propTypes?: WeakValidationMap<P>;
    }
}

type A<T1> = <T2>() => React.FunctionComponent<T1 & T2>;

function B<T>(_: A<T>) {}

interface C {
    r: String;
}

function myFunction<T2>(): React.FunctionComponent<C & T2> {
    return {};
}

B(myFunction) // Error
B<C>(myFunction) // No error

Expected behavior: In v3.8.3, the code above did not have any compilation error.

Actual behavior: Compilation error TS2345 when I do not explicitly pass the generic type.

Playground Link: Link

Note: I use React, here I only included the minimal React interfaces to reproduce the issue. The issue is also reproducible with React code directly here

Activity

  1. weswigham commented on Jun 23, 2020

    @weswigham
    Member

    Looks to be caused by #37261 - the erased signature seems ok, in theory, but because it erases to any, any & T1 becomes just any, and we lose the ability to infer to T1. If we erase to unknown instead, this should work - unknown will still behave like any in unions still, though (and prevent inference to other union members), which makes me wonder if erasing to a unique anti-any-type-thing might be better - something which evaporates in both unions and intersections, but otherwise behaves anyish.

  2. davidje13 commented on Jul 15, 2020

    @davidje13

    I'm wondering if this is the same issue (also works in 3.8 and fails in 3.9):

    interface Input<V> { v: V }
    const inner = <V>(fn: <T extends Input<V>>(t: T) => null) => null;
    export default () => inner(<T extends Input<string>>(t: T) => null); // <-- error here

    Calling inner<string>(...) explicitly works.

    This shorter example has the same general structure (an argument which is a function with a complicated parameter), but there's too much going on for me to be sure it's the same bug.

  3. 1 remaining item

  4. RyanCavanaugh commented on Feb 15, 2023

    @RyanCavanaugh
    Member

    This behaves as expected now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions