# exactlyOne

Source: https://taxilang.org/docs/language/stdlib/exactlyOne

**Full name:** `taxi.stdlib.exactlyOne`

## Signature

```taxi
declare extension function <T> exactlyOne(collection:T[]):T
```

## Description

Returns the only item from the provided collection, or throws an error if there isn't exactly one item in the collection.

This function is useful when you expect a collection to contain precisely one element and want to retrieve it.

```taxi
// Get the single active session for a user
find { user: User } as {
   currentSession: user.sessions.filter((session) -> session.isActive).exactlyOne()
}
```

## Examples

This example demonstrates how to use `exactlyOne()` to retrieve the single active session for a user.

Schema:

```taxi
model Session {
   id: SessionId inherits String
   isActive: IsActive inherits Boolean
}

model User {
   username: Username inherits String
   sessions: Session[]
}
```

Query:

```taxi
given {
   user: User = {
      username: 'ActiveUser',
      sessions: [
         { id: 'session-1', isActive: true },
         { id: 'session-2', isActive: false }
      ]
   }
}
find {
   username: user.username
   activeSessionId: SessionId = user.sessions.filter((Session) -> IsActive).exactlyOne().id
}
```

Result:

```json
{
  "username": "ActiveUser",
  "activeSessionId": "session-1"
}
```
