How to fix Type predicate function does not correctly narrow type in TypeScript | DevErrors