Skip to content

3.5 regression: union type as property accessor break function parameter inference #32038

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

Closed
fregante opened this issue Jun 22, 2019 · 4 comments · Fixed by #32049
Closed

3.5 regression: union type as property accessor break function parameter inference #32038

fregante opened this issue Jun 22, 2019 · 4 comments · Fixed by #32049
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@fregante
Copy link

fregante commented Jun 22, 2019

TypeScript Version: 3.5.2 (it works with 3.4.4). Also tested on 3.6.0-dev.20190622

Search Terms:

Code (not real-life, just a repro)

const actions = ['resizeTo', 'resizeBy'] as const;
for (const action of actions) {
	window[action] = (x, y) => {
		window[action](x, y);
	}
}

Expected behavior:

x and y are of type number (like in 3.4)

Actual behavior:

x and y are of type any

Playground Link: (enable noImplicitAny, but it seems to be on 3.4)

https://www.typescriptlang.org/play/#src=%0D%0Aconst%20actions%20%3D%20%5B'resizeTo'%2C%20'resizeBy'%5D%20as%20const%3B%0D%0Afor%20(const%20action%20of%20actions)%20%7B%0D%0A%09window%5Baction%5D%20%3D%20(x%2C%20y)%20%3D%3E%20%7B%0D%0A%09%09window%5Baction%5D(x%2C%20y)%3B%0D%0A%09%7D%0D%0A%7D

Related Issues:

#32037

@fregante
Copy link
Author

fregante commented Jun 22, 2019

This is probably due to #30769

But the types of window.resizeTo and window.resizeBy are identical so there shouldn't be any difference or ambiguity.

You can find a real-life example here: refined-github/refined-github#2164

@ahejlsberg
Copy link
Member

Yes, this is caused by #30769. When an indexed access type occurs on the target side of an assignment, we form an intersection instead of a union. When intersecting properties of the same type we just end up with a property of that type, however we don't remove duplicate call/construct signatures. So, in your example the intersection ends up being a type with two (identical) call signatures, at which point we no longer do contextual typing of parameters.

We can fix the issue by removing duplicate call/construct signatures in an intersection.

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Jun 23, 2019
@ahejlsberg ahejlsberg self-assigned this Jun 23, 2019
@ahejlsberg ahejlsberg added this to the TypeScript 3.6.0 milestone Jun 23, 2019
@fregante
Copy link
Author

Great!

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jul 1, 2019
@fregante
Copy link
Author

fregante commented Jul 1, 2019

I can confirm that this was fixed and works as expected in 3.6.0-dev.20190701

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants