TS7034

error TS7034: Variable ‘expectations‘ implicitly has type ‘any[]‘ in some locations where its type cannot be determined.

Broken Code ❌

1
const expectations = [];

Fixed Code ✔️

An array can collect values of different types, so we have to tell TypeScript which types we want to collect:

1
const expectations: string[] = [];

If we want to specify multiple types, we have to define a union type:

1
2
3
const expectations: (string | number)[] = [];
expectations.push('1');
expectations.push(2);

Alternative:

1
2
3
4
type ArrayInput = string | number;
const expectations: ArrayInput[] = [];
expectations.push('1');
expectations.push(2);

Unrecommended solutions: