Skip to content

Only consider function parameter types for generic type inference as a last resort #810

Closed
@RyanCavanaugh

Description

@RyanCavanaugh

This is a rough sketch so I have something to refer to.

Currently, we treat the two instances of T in the parameter list here equally:

declare function containsAny<T>(arr: T[], predicate: (x: T) => boolean): boolean;

This means that when we make a call like this:

var x = containsAny([1, 2, 3], 'hello');

we infer T to be {}. The first argument is assignable to {}[] and the second argument is assignable to {}, so the call succeeds. This is unexpected and basically wrong.

If instead we only used the type parameters from function arguments when there were no other candidates for a given type parameter, we would infer T to be number and the call would be correctly rejected.

Not done yet: Actual spec work, thinking it through all the way, testing against existing code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions