TS7034

Variable 'expectations' implicitly has type 'any[]' in some locations where its type cannot be determined.

Broken Code ❌

const expectations = [];

Fixed Code ✔️

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

const expectations: string[] = [];

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

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

Alternative #1:

Create a type alias and provide a type annotation for the expectations constant:

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

Alternative #2:

Use concrete values to initialize the expectations array, so TypeScript can infer the array type for you:

const expectations = [-1, '0'];
expectations.push('1');
expectations.push(2);

Unrecommended solution:

  • Set "noImplicitAny" to false in your "tsconfig.json"