Avatar ·

Error "not assignable to parameter of type never" in TypeScript: causes and solution

Hello colleagues! I'm new to TypeScript and encountered a confusing error. I'm writing a simple function that filters an array of strings, keeping only those longer than 3 characters. Here's the code: ```typescript function filterShortStrings(arr: string[]): string[] { return arr.filter((item) => { if (item.length > 3) { return item; } }); } const result = filterShortStrings(['cat', 'elephant', 'dog', 'giraffe']); console.log(result); ``` TypeScript throws an error on the line `return item;`: ``` Argument of type 'string' is not assignable to parameter of type 'never'. ``` I don't understand what "never" means in this context. After all, `item` is a string, and I'm returning it from the `filter` callback. Why does TypeScript think I'm trying to return something incompatible? Maybe I'm not