TS2677

error TS2677: A type predicate’s type must be assignable to its parameter’s type. Type ‘number‘ is not assignable to type ‘string‘.

Broken Code ❌

1
2
3
const isInteger = (input: string): input is number => {
return !!parseInt(input, 10);
};

Fixed Code ✔️

The input is declared to be of type string which is why the type predicate cannot turn it into a number because these two declarations are mutually exclusive. That’s why we have to declare an input type of any:

1
2
3
const isInteger = (input: any): input is number => {
return !!parseInt(input, 10);
};