-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Unsimpilifed Interesction Types Prevent Type Inference #12289
Comments
What's the reason for the |
@ahejlsberg This came up in #12253, where I was trying to update the type declarations for keys<T extends { [key: string]: any }>(o: T): (keyof T)[]; The problem was that let y: { [x: string]: any } = {};
// ... add some keys to y.
Object.keys(y).map(it => it.charAt(0)) // error, charAt is not on number, and `it` is string | number Note that the code above is just a simplified version of some code from the compiler, which wouldn't build after I tried the above type declaration. So, my effort to fix it was to use: keys<T extends { [key: string]: any }>(o: T): (keyof T & string)[]; That makes the code above type check, because the type of |
Here's how you'd write interface ObjectConstructor {
// ...
keys<T>(obj: T): (keyof T)[];
} There's no need for a constraint on let o: { x: number, y: number };
o = { x: 1, y: 2, z: 3 };
let keys = Object.keys(o); // Would have type ('x' | 'y')[] which isn't correct |
The problem with that is it won't work for primitives (e.g.
Ahh. I hadn't thought about that, and it does seem like a big problem. Does that mean we should give up on using |
TypeScript Version: 2.2.0-dev.20161112
Code
Expected behavior:
handlerA
andhandlerB
should have the same type inferred (becauseMethod
andMethod2
should be treated as the same type, by simplifyingMethod2
down toMethod
).Actual behavior:
handlerA
is inferred correctly;handlerB
errors/is implicitly any.For context, here's the real code (and my attempt to fix it) that produced this problem: #12253 (comment)
The text was updated successfully, but these errors were encountered: