Sobes.tech
Middle

Which array method breaks type safety in TypeScript?

sobes.tech AI

Answer from AI

The array method that breaks type safety in TypeScript is Array.prototype.push. It returns the new length of the array, not the array itself, which can sometimes lead to errors if you expect a chain of calls or a different result type.

Example:

const arr: number[] = [1, 2, 3];
const result = arr.push(4); // result has type number (new length), not number[]

Trying to use push in a chain of calls will cause a TypeScript error because the return value does not match the expected array type.