Properly use foreach loop in TypeScript