Skip to content

Type inference has failed by parameter order (regression) #19858

Description

@falsandtru

Intended degradation?

cc Anders Hejlsberg (@ahejlsberg)

TypeScript Version: master

Code

declare function f<a>(as1: a[], as2: a[], cmp: (a1: a, a2: a) => number) 
f(Array.from([0]), [], (a1, a2) => a1 - a2);
f(Array.from([]), [0], (a1, a2) => a1 - a2);

Expected behavior:

no error

Actual behavior:

$ node built/local/tsc.js index.ts -t es6 --strictNullChecks
index.ts(3,36): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
index.ts(3,41): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

Activity

  1. mhegazy commented on Nov 9, 2017

    @mhegazy
    Contributor
    c:\test\sandbox7>type a.ts
    declare function f<a>(as1: a[], as2: a[], cmp: (a1: a, a2: a) => number): void;
    
    f(Array.from([0]), [], (a1, a2) => a1 - a2);
    f(Array.from([]), [0], (a1, a2) => a1 - a2);
    
    c:\test\sandbox7>tsc --v
    Version 2.7.0-dev.20171109
    
    c:\test\sandbox7>tsc a.ts --lib es6
    
    c:\test\sandbox7>echo %ERRORLEVEL%
    0
  2. mhegazy commented on Nov 9, 2017

    @mhegazy
    Contributor

    I think your example is not correct, and you are looking at a result of #19745.

  3. falsandtru commented on Nov 9, 2017

    @falsandtru
    ContributorAuthor

    c:\test\sandbox7>tsc a.ts --lib es6

    You have to enable strictNullChecks option.

  4. falsandtru commented on Nov 9, 2017

    @falsandtru
    ContributorAuthor

    you are looking at a result of #19745.

    Seems like your are right but this result is bad. Anders Hejlsberg (@ahejlsberg) Is this your intended result?

  5. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    Needs More InfoThe issue still hasn't been fully clarified
    on Nov 9, 2017
  6. gcnew commented on Nov 10, 2017

    @gcnew
    Contributor

    #19745 fixes a legitimate issue. However, I do agree that the exhibited behaviour here is also a bug.

    The problem is that Array.from([]) should either be:

    • contextually typed
    • inferred as <T> T[], i.e. an array of a "to be specified" generic type

    Currently the call to Array.from is eagerly resolved to Array.from<{}> while before it was incorrectly resolved to Array.from<never>.

    In any case, the root cause is #9366.

  7. ahejlsberg commented on Nov 10, 2017

    @ahejlsberg
    Member

    It is indeed a regression caused by #19745 where we make no inferences from the never element type of an empty array literal. Another way we could solve #19576 (the issue addressed by #19745) is to give inferences from the element type of empty array literals lower priority than all other inferences. That would still fix #19576 but wouldn't affect the code in this issue.

  8. added
    BugA bug in TypeScript
    and removed
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    on Nov 10, 2017
  9. falsandtru commented on Nov 10, 2017

    @falsandtru
    ContributorAuthor

    Thanks!

  10. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions