-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inconsistency in types after type inference #24369
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
Your interface says that the interface IProcess<X, Y> {
start(): X;
process(opt: [X]): Y;
finish(opt: [Y]): void;
}
function foo<X, Y>(desc: IProcess<X, Y>): void {
const x = desc.start();
const y = desc.process([x]);
desc.finish([y]);
}
foo({
start() {
return {
name: "Joe"
};
},
process([x]) {
return x.name;
},
finish([x]) {
console.log(x);
}
}); Here, |
We do not treat method signatures as contextually sensitive.. i am not quite sure if this was a deliberate choice or a bug.. so this works as expected: foo({
start: () => {
return {
name: "Joe"
};
},
process: ([x]) => {
return x.name;
},
finish: ([x]) => {
console.log(x);
}
}); The error on process<XX extends X = X>(opt: F<XX>): Y; |
Nonsense! Inference for |
Thanks for your comments.
Thinking what would be the best way to rephrase my use-case from the ticket description.. I want users of my lib not to have to specify types while getting benefits of type inference OR confirm here that such expectations of inference in typescript should not be there :) I'm expecting more advance inference based on: little knowledge of how inference works in TS, previous experience working with haskell.
This is exactly what I was looking for in the docs but didn't manage to find 🤔
This would solve the inconsistency issue, 👍 |
Another case where having a specification for TypeScript would be helpful. 🤔 |
I'm pretty sure we do: function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
return (isInJavaScriptFile(func) && isFunctionDeclaration(func) || isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) &&
isContextSensitiveFunctionLikeDeclaration(func);
}
I'm wondering if this is an instance where in which "pulling" on the type of |
Tagging this as design limitation unless @weswigham or @ahejlsberg has ideas about a targeted fix. In general we are not great at inference that requires knowing which order things needs to happen in. Or we wait for #30134 |
I'm currently working on providing a nice API in a library where users will be providing implementations of a particular interface. Interface's methods are meant to be executed async in a particular order. Result of calling one method will be transformed in an known way and passed to the next method.
In order to not force my users typing signatures again and again for every part of the process in type parameters, I wanted to benefit from type inference and not to have specify types at all, while still having proper type checking.
Example below illustrates the intention.
While trying things out — faced some inconsistencies in typescript (or VS Code?) which IMHO worth reporting.
TypeScript Version: 2.9.0-dev.201xxxxx
Search Terms: "inference"
Code
Expected behavior:
In
process
functionx.name
would be of a typestring
Actual behavior:
Property 'name' does not exist on type 'XX'.
var x: XX = { name: string }
Playground Link: playground link
Related Issues: didn't find any
The text was updated successfully, but these errors were encountered: