Evolving Any

What

“Evolving Any” is a special type inference feature where an empty array initialized without an explicit type begins as any[] but “evolves” its type as you push values into it.

How

When you initialize an empty array, it starts broad. As you add data, TypeScript narrows the inferred type:

let inventory = []; // starts as any[]

inventory.push(42); 
// inventory is now inferred as number[]

inventory.push("robe"); 
// inventory is now inferred as (number | string)[]

Why

This feature provides flexibility during the initial construction of a data structure. However, once the array is passed out of its initial scope (e.g., returned from a function), its type “locks” and stops evolving, ensuring that the rest of your application interacts with a predictable type.

Summary

While convenient, it is usually better practice to explicitly type your arrays (e.g., let inventory: string[] = []) to prevent accidental type pollution.