Map Left

advanced8 of 8

Learn about map left in fp-ts either

Code Editor

08-map-left.exercise.ts

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

Test Results

Requirements

describe('enrichError', () => {
  const userRight: E.Either<ErrorCode, User> = E.right({ id: 1, name: 'Alice' })
  const userLeft1: E.Either<ErrorCode, User> = E.left('ERR_NOT_FOUND')
  const userLeft2: E.Either<ErrorCode, User> = E.left('ERR_INVALID_ID')

  it('preserves right value', () => {
    const result = enrichError(userRight)
    expect(result).toEqual({ _tag: 'Right', right: { id: 1, name: 'Alice' } })
  })

  it('enriches ERR_NOT_FOUND error', () => {
    const result = enrichError(userLeft1)
    expect(result._tag).toEqual('Left')
    if (result._tag === 'Left') {
      expect(result.left.code).toEqual('ERR_NOT_FOUND')
      expect(result.left.message).toEqual('User not found')
      expect(typeof result.left.timestamp).toBe('number')
    }
  })

  it('enriches ERR_INVALID_ID error', () => {
    const result = enrichError(userLeft2)
    expect(result._tag).toEqual('Left')
    if (result._tag === 'Left') {
      expect(result.left.code).toEqual('ERR_INVALID_ID')
      expect(result.left.message).toEqual('Invalid user ID')
      expect(typeof result.left.timestamp).toBe('number')
    }
  })
})
🧪

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