-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Weird type-widening issue when using methods instead of function properties. #6309
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
Comments
This doesn't have to do with widening. The problem is that you typed You'll see the version that errors the same way here. |
Thanks, I enabled Out of interest, is there a way I can make this program typecheck without For example, I can add explicit type annotations to make it work, like in this version, but could tsc not infer that itself based on the return type of the |
Part of it is the way we select inference points, and the flow of direction between types. Supporting many different inference sites can be complex. One way to get what you want is to make function fooIsEq<T extends { foo: number }>(x: number) {
return new Predicate<T, boolean>(v => v.foo === x);
}
function barIsEq<T extends { bar: number }>(x: number) {
return new Predicate<T, boolean>(v => v.bar === x);
}
function fooAndBarEq(x: number) {
return fooIsEq<{foo: number; bar: number }>(42)
.chain(() => barIsEq(42));
} Another approach is to say that class Predicate<T, X> {
constructor(private _run: (x: T) => X) {}
public run: (x: T) => X = (v: T) => {
return this._run(v);
}
public chain<U, Y>(f: (x: X) => Predicate<U, Y>) {
return new Predicate((v: U & T) => f(this.run(v)).run(v));
}
}
function fooIsEq(x: number) {
return new Predicate<{foo: number}, boolean>(v => v.foo === x);
}
function barIsEq(x: number) {
return new Predicate<{bar: number}, boolean>(v => v.bar === x);
}
function fooAndBarEq(x: number) {
return fooIsEq(42).chain(() => barIsEq(42));
} This will allow you to compose predicate functions easily, but you're more prone to accidentally passing in an incorrectly typed function. By the way, does |
Thanks a lot. The I'd be especially interested in how co & contravariance are solved and how it interacts with the bivariance of function arguments. Is that why you were saying "[I was] more prone to accidentally passing in an incorrectly typed function." with this approach? |
We're in the process of documenting things a little more, and the spec will have it soon as well. What I meant is that you can |
Hi there.
I'm using the tsc compiler, version 1.8.0-dev.20151231, and I'm running into a weird issue where widening of structural types works when I'm defining a class with a function property, and fails when I'm replacing that function property by a "proper" method.
But see for yourself: This is the version that compiles, and this is the version that doesn't.
What is the difference here? Is this a bug in the type inference engine?
The text was updated successfully, but these errors were encountered: