Fold

beginner3 of 8

Learn about fold in fp-ts either

Code Editor

03-fold.exercise.ts

💻
Loading editor...
Preparing Monaco Editor with TypeScript support

Test Results

Requirements

describe('getUserName', () => {
  const userRight: E.Either<UserError, User> = E.right({ id: 1, name: 'Alice' })
  const userLeft1: E.Either<UserError, User> = E.left('Invalid ID')
  const userLeft2: E.Either<UserError, User> = E.left('User not found')

  it('returns user name for right value', () => {
    const result = getUserName(userRight)
    expect(result).toEqual('Alice')
  })

  it('returns error message for left value - Invalid ID', () => {
    const result = getUserName(userLeft1)
    expect(result).toEqual('Invalid ID')
  })

  it('returns error message for left value - User not found', () => {
    const result = getUserName(userLeft2)
    expect(result).toEqual('User not found')
  })
})
🧪

Ready to Test?

Click "Run Tests" to see how your code performs

Pro Tips

💡 Stuck? Here's what to try:

  • • Read the comments in the code carefully
  • • Run tests frequently to get feedback
  • • Check the fp-ts documentation
  • • Use the solution if you need help

🚀 Learning Approach:

  • • Focus on understanding, not just solving
  • • Experiment with different approaches
  • • Think about real-world applications
  • • Build on previous exercises