-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 parameter inference not working with union types #19634
Comments
This is one of the pitfalls of a structural type system -- the |
Oh yea, sure enough. If I put an explicit return type of Still seems weird though... |
Basically, the |
duplicate of #1373 |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.7.0-dev.20171031
Code
Expected behavior:
I have a simple setup for the visitor pattern, but with a generic interface so that I can create visitors that return different types. This works perfectly if I implement the interface with a single type.
However, I have a situation where I have two types of "visitee" classes for a particular visitor. One type will return a
number
, and the other will returnvoid
. So I decided I would specifynumber | void
as the return type of the visitor. I am fine with having to do a cast when calling a number visitee from a void visitee, as I do above.I would expect that when I call
thing.other.visit(this)
, the type parameter forvisit<T>()
would use the type ofSomeVisitor
, which isIVisitor<number | void>
, andT
would infer tonumber | void
.Actual behavior:
However, for some reason, the type for
T
is inferred to bevoid
, which leads me to believe that some other mechanism is being used to determine the type parameter. Sincenumber
is not assignable tovoid
, the cast tonumber
fails.I can work around this by specifying the type parameter explicitly:
or casting
this
to the expected visitor type:but I believe that TypeScript should be able to correctly infer the type given the information that it has.
The text was updated successfully, but these errors were encountered: