Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Type parameters do not get fixed when the compiler visits the same signature multiple times #2377

Closed
JsonFreeman opened this issue Mar 16, 2015 · 3 comments
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created

Comments

@JsonFreeman
Copy link
Contributor

As of change #2356, the following code gives the correct result in the batch compiler:

declare function f<T>(x: T, y: (p: T) => T, z: (p: T) => T): T;
f(0, x => null, x => x.blahblah);

Namely, we infer T to be number, and we get an error on blahblah.

However, there are a few things still wrong:

  • The language service does not show the correct result. It shows both x's to be number, but if you hover on f, you will see that it was instantiated to any, instead of number.
  • If you add another overload, the wrong type argument gets inferred:
declare function f<T>(x: T, y: (p: T) => T, z: (p: T) => T): T;
declare function f();
f(0, x => null, x => x.blahblah);

Now suddenly blahblah is not an error, and the second x is any.

  • In theory, if this overload is not assignable, but it has already fixed the type parameter T, and another overload comes along with a type parameter in the same position, that second type parameter will not get fixed. I don't have a good repro for this, but it is certainly a possibility in theory.

All of these issues happen because when a function expression parameter causes a type parameter to get fixed, it only does so if it is affected by the type of that type parameter. I believe the right thing here is that even if a function parameter does not align with the type parameter, it should still fix it. That way, it will fix the type parameter no matter which signature is being visited, or whether this signature has been visited before.

I consider this bug a followup to bugs #2127 and #2182.

@JsonFreeman JsonFreeman added the Bug A bug in TypeScript label Mar 16, 2015
@DanielRosenwasser
Copy link
Member

Is this bug different from #2378?

@JsonFreeman
Copy link
Contributor Author

Whoa, not sure how that happened! They are exactly the same.

@JsonFreeman JsonFreeman added the Duplicate An existing issue was already created label Mar 17, 2015
@JsonFreeman
Copy link
Contributor Author

Duplicate of #2378

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants