-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Unused type parameter should be indefered as {}
#15392
Comments
It also seems that problem persists even class does not use type parameter here is a refactored example: // @flow
class Future<x, a> {
constructor(private execute: (succeed: (value: a) => void, fail: (error: x) => void) => number | void) {
}
fork(succeed: (value: a) => void, fail: (error: x) => void) {
this.execute(succeed, fail)
}
}
class Fail<x> {
error:x
constructor(error: x) {
this.error = error
}
fork <a> (succeed: (value: a) => void, fail: (error: x) => void) {
fail(this.error)
}
}
const fail = <x, a> (error: x):Task<x, a> =>
new Fail(error)
type Task<x, a> =
| Fail<x>
| Future<x, a>
const task:Task<number, void> = fail(4) // <- Type 'Task<number, {}>' is not assignable to type 'Task<number, void>'. Which still has same error:
But it type checks fine if following code if const task:Task<number, void> = new Fail(4) Presumably that's due to |
If a type parameter has no inference location (and no default), then inference gives it the upper bound which is if you would want that type parameter to match anything if not specified, consider adding a default to it as The problem here seems to be the same as #11152. the generic type is only referenced in return type position, which is currently not an inference position, #11152 tracks addressing that. |
@mhegazy Thanks for the clarification.
It's more like I presume that if
Does that mean that once #11152 is fixed both examples would work as expected ? Or would using |
The rational for this is that a type parameter with no inference location is suspect. setting it to
#11152 does not change the general case, unmatched type parameters will still be |
Also is |
Never mind I found answer to my question https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#generic-parameter-defaults |
TypeScript Version: 2.2.2
Code
Expected behavior:
I would expect
Task.fail(4)
to be inferred asTask<number, *>
where*
type is open for further inference. In other words it should be able to satisfyTask<number, void>
orTask<number, number>
or whatever else maybe thrown at it.It is also worth mentioning that same code type checks in flow as expected.
Actual behavior:
Unused type parameter is inferred as
{}
making causing a type checker to fail on commented line with a following error:The text was updated successfully, but these errors were encountered: