Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
falsandtru opened this issue Nov 9, 2017 · 7 comments
Closed

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

falsandtru opened this issue Nov 9, 2017 · 7 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@falsandtru
Copy link
Contributor

Intended degradation?

cc @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.
@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2017

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

@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2017

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

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Nov 9, 2017
@falsandtru
Copy link
Contributor Author

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

You have to enable strictNullChecks option.

@falsandtru
Copy link
Contributor Author

falsandtru commented Nov 9, 2017

you are looking at a result of #19745.

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

@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Needs More Info The issue still hasn't been fully clarified labels Nov 9, 2017
@gcnew
Copy link
Contributor

gcnew commented Nov 10, 2017

#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.

@ahejlsberg
Copy link
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.

@ahejlsberg ahejlsberg self-assigned this Nov 10, 2017
@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Nov 10, 2017
@falsandtru
Copy link
Contributor Author

Thanks!

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Nov 10, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants