How to fix Type guard does not narrow type 'X' in TypeScript | DevErrors