Open
Description
TypeScript Version: 3.7-Beta, "noImplicitAny" compiler mode
Search Terms:
Array.prototype.reduce, reduce type inference, reduce implicit any
Code
const result = [1].reduce((acc, item) => {
acc.push(item)
return acc
}, [])
result[0].toLowerCase()
Expected behavior:
result
implicitly has typeany[]
-> compiler error ("noImplicitAny")
orresult
has typeunknown[]
-> compiler error
Actual behavior:
result
has type any[]
, no errors
Related Issues: #25454, #29604
P.S. It seems reasonable to align behavior with other empty array cases, e.g.:
let x = {
array: [] // Object literal's property 'array' implicitly has an 'any[]' type
}
let x = []
x[0].toLowerCase() // Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
RyanCavanaugh commentedon Oct 30, 2019
We should try to write a new definition of
reduce
that produces the desired behavior in all cases