-
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
Infer variable type form return type #16218
Comments
What if you wanted to do this? function f(): ReadonlyArray<Animal> {
const res = [];
res.push(new Cat());
res[0].meow();
return res;
} If we infer In the particular case you're looking at, we actually will infer the type of function f() {
const res = [];
res.push(1);
return res;
} Here we infer |
In addition to Andy's point, this would be a pretty big change in our type system. We don't really do inference going "backwards" from latter statements. The type system works off of a declared type which is inferred from its declaration. That type is specialized by subsequent checks and assignments. While there are the concepts of type argument inference and contextual types (which I mention because they have a sort of back-and-forth interaction), those are limited to the statement level. |
@andy-ms your examples are unclear to me; as both will result in compile errors (with strict null checks enabled). With out them, the infered type of
EDIT: The examples (suprisinginly?) do not give compile errors if @DanielRosenwasser I can imagine this is hard to solve generically, but I figured it would be pretty convenient, especially where a variable type could be inferred directly from the return statements, and normal type inference cannot deliver a "good" candidate. Not being able to infer a good candidate is probably limited to empty arrays and empty object literals: This annoyance is especially noticable with arrays; as A similar case is where a lookup map (object) is returned, but initialized as |
That does seem like a bug; I would expect that with implicit |
With |
@ahejlsberg, @DanielRosenwasser thanks for the explanation. So, the The inference without |
Closing the issue as the original question is answered. (The inferences of |
TypeScript Version: 2.3.2
In many cases I have a variable that holds a value to be eventually returned from the function. The type of this variable always matches the return type of the function. It would be awesome if the type of that variable can be inferred from the fact that it is returned, using
return
, from the function, if the function has an explicitly declared return type.I am aware that inference is done the other way around, however, we always work with explicit return types on functions. We have linting rules enforcing this, as it is a good practice that helps code reviews and encourages api-first thinking.
Code
So currently I have to write
Expected behavior:
Actual behavior:
Compile error:
'Variable 'res' implicitly has type 'any[]' in some locations where its type cannot be determined.'
The text was updated successfully, but these errors were encountered: