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

Issue with js.Promise.then() type inference #6790

Closed
jcward opened this issue Dec 20, 2017 · 3 comments
Closed

Issue with js.Promise.then() type inference #6790

jcward opened this issue Dec 20, 2017 · 3 comments
Assignees
Milestone

Comments

@jcward
Copy link
Contributor

jcward commented Dec 20, 2017

There is a lot of type magic (Either's, parameters) in js/Promise.hx to mirror the behavior of JavaScript's Promise. Unfortunately, somewhere in all this magic, we lose type inference to the argument of the then callback.

Promise.then() type inference not working: https://try.haxe.org/#aa552

NotAPromise.then() type inference IS working: https://try.haxe.org/#b052a

My brain cannot wrap through all the type magic to know whether this is expected or not, or which exact feature breaks the inference, but from an end user perspective, I'd like Promise.then() to work as well as NotAPromise.then()

@jcward jcward changed the title Issue with js.Promise.then() inference Issue with js.Promise.then() type inference Dec 20, 2017
@nadako
Copy link
Member

nadako commented Dec 20, 2017

This is a more general issue with unifying functions against abstracts (which EitherType is):

abstract Callback(Dynamic) from Int->Void {}

class Main {
	static function main() {
		var f:Int->Void = function(t) $type(t); // Int
		var f2:Callback = function(t) $type(t); // Unknown<0>
	}
}

Probably comes from the fact that with abstracts there can be multiple from/@:from, so we can't simple use one of them as the "expected" when typing the function, so it's typed on its own first and then an abstract cast is chosen.

For the js.Promise case there might be another way to deal with it using @:overload instead of EitherType, I'll look into it.

@nadako nadako self-assigned this Dec 20, 2017
@jcward
Copy link
Contributor Author

jcward commented Dec 21, 2017

Thanks for looking into this!

(I decided to separate the 'empty promise' request into its own issue, #6793)

@jdonaldson
Copy link
Member

jdonaldson commented Dec 21, 2017

Here's some typescript folks struggling with the same issue:
microsoft/TypeScript#8516
microsoft/TypeScript#3356
microsoft/TypeScript#4260
DefinitelyTyped/DefinitelyTyped#6091

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants