# For Typescript Users

All mitter.io web/javacsript SDKs are written in typescript and as such are bundled with typings automatically. This section details on how to optimally use the mitter.io libraries if you are using typescript.

### Type-matching functions

If you are using type-matching functions to get the type of a payload for example, the functions are implemented as predicates, so typescript will automatically cast it within the branch. For example:

```javascript
mitter.subscribeToPaylod(payload => {
    if (payload['@type'] === 'NewMessagePayload') {
        // The next line will throw an error, since the type of
        // payload is MessagingPipelinePayload
        console.log('New message', payload.message.textPayload)
    } else {
        // The next will not throw an error, since the callback
        // argument is already typed to MessagingPipelinePayload
        console.log('New payload', payload.globalPipelinePayloadId)
    }
})

```

Instead, if you were to use the bundled type-matching functions:

```javascript
mitter.subscribeToPayload(payload => {
    if (isNewMessagingPayload(payload)) {
        // The next line is OK, since the isNewMessagingPayload is a type
        // predicate, which tells the compiler that if true, then the
        // argument (payload) was of type NewMessagingPayload
        console.log('New message', payload.message.textPayload)
    }
})
```

### API calls using clients

All bundled clients are typed and will throw an error when using incorrect request/response values

```javascript
mitter.clients().channels().newChannel({
    // ERROR. Missing property 'defaultRuleSet'
    channelId: 'my-new-channel'
})

mitter.clients().channels().newChannel(new Channel(...))
    // ERROR. No property `wrongProperty` on type Channel
    .then(x => console.log('New channel', channel.wrongProperty)
```

> **NOTE** The above does not apply if you are using fetch and/or axios to make API calls. The internal clients that are used with [restyped](https://github.com/rawrmaan/restyped) are exposed in case you wish to use them with your own axios clients. Do refer to the ts-docs bundled with `@mitter-io/core` on how to access these objects.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mitter.io/sdks/web/for-typescript-users.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
