Closed
Description
TypeScript Version: 3.6.2
Search Terms:
- 7022
- directly or indirectly in its own initializer
Code
function extent(nums: number[]) {
let result: [number, number] | null = null;
for (const num of nums) {
if (!result) {
result = [num, num];
} else {
const [oldMin, oldMax] = result;
result = [Math.min(num, oldMin), Math.max(num, oldMax)];
}
}
return result;
}
Expected behavior:
No error.
Actual behavior:
'oldMin' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)
'oldMax' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)
I'm really struggling to see where the circularity comes in. The type of result
is shown as [number, number]
in that branch, so I'd assume destructuring it would result in two number
types.
Removing the destructuring and using array accesses is equivalent but produces no error:
function extent(nums: number[]) {
let result: [number, number] | null = null;
for (const num of nums) {
if (!result) {
result = [num, num];
} else {
result = [Math.min(num, result[0]), Math.max(num, result[1])]; // ok
}
}
return result;
}
Playground Link: link
Related Issues: