Description
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
function findReconcilationCandidates(snapshot: any): Node[] {
const res: Node[] = []
// push some items on res
return res
}
Expected behavior:
function findReconcilationCandidates(snapshot: any): Node[] {
const res = [] // type is inferred from return statement
// push some items on res
return res
}
Actual behavior:
Compile error:
'Variable 'res' implicitly has type 'any[]' in some locations where its type cannot be determined.'