> For the complete documentation index, see [llms.txt](https://docs.opensoutheners.com/oss/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opensoutheners.com/oss/flex-url/advanced/typed-schemas.md).

# Typed Schemas (TypeScript)

Narrow the TypeScript builder to a specific endpoint's allowed filters, sorts, includes, fields, and appends at compile time.

{% hint style="info" %}
This is a TypeScript-only feature. PHP has no compile-time generics, so `FlexUrl::make()`/`::from()` accept only a `$url` and `FlexUrlOptions` — there is no schema-narrowing equivalent on that side.
{% endhint %}

`flexUrl<S extends EndpointSchema>(path, schema?)` narrows `filter()`/`sort()`/`include()`/ `fields()`/`append()` arguments to a specific endpoint's allowed vocabulary at compile time. `EndpointSchema` is the shared contract generated by apiable's `apiable:types` exporter, so the schema doesn't need to be hand-written in most projects.

```ts
import {flexUrl, type EndpointSchema} from 'flex-url';

interface IssuesSchema extends EndpointSchema {
  resource: 'issues';
  path: '/api/v1/issues';
  filters: {
    status: {operators: ['equal']; values: ['open', 'closed']};
    due_at: {operators: ['gte', 'lte']};
  };
  sorts: ['priority', 'due_at'];
  includes: ['project', 'assignee'];
  fields: {issues: ['title', 'status', 'priority']};
  appends: {issues: ['is_overdue']};
}

// S is inferred from the `schema` argument — no explicit generic needed.
const issues = flexUrl('/api/v1/issues', issuesSchema);

issues.filter('status', 'open');   // OK
issues.filter('budget', '100');    // compile error: "budget" isn't in IssuesSchema['filters']

// Or narrow with an explicit generic and no runtime schema object:
const alsoIssues = flexUrl<IssuesSchema>('/api/v1/issues');
```

## What gets narrowed

| Call                                      | Narrowed to                                                 |
| ----------------------------------------- | ----------------------------------------------------------- |
| `filter(attribute, ...)`                  | `keyof schema.filters`                                      |
| `filter(attribute, operator, value)`      | that attribute's registered operators (plus the `eq` alias) |
| `sort(attribute)` / `sortDesc(attribute)` | `schema.sorts[number]`                                      |
| `include(...relationships)`               | `schema.includes[number]`                                   |
| `fields(type, ...columns)`                | `keyof schema.fields`, then that type's columns             |
| `append(type, ...accessors)`              | `keyof schema.appends`, then that type's accessors          |

A schema's `scope`-operator filters are excluded from the narrowed operator union for `filter()` — use [`filterScope()`](https://docs.opensoutheners.com/oss/flex-url/building-urls/filters.md#scoped-filters) for those, which isn't schema-narrowed.

## Dev-mode diagnostics

Passing a **runtime** `schema` object (not just the generic) additionally enables dev-mode diagnostics: an unrecognised filter/sort/include/fields/appends call logs a `console.warn` — it never throws or changes behaviour — useful for catching a typo that an untyped call site wouldn't catch at compile time (e.g. a value read from user input, or a dynamically-built attribute name).

Omit the schema argument (explicit-generic-only usage, `flexUrl<IssuesSchema>(path)`) to skip these warnings entirely — there's no runtime object to check against.

## No schema at all

`flexUrl(path)` with no schema argument and no explicit generic accepts any `string` for every attribute/type/operator argument — the unnarrowed, `EndpointSchema | undefined = undefined` default. This is the same builder used throughout the rest of these docs.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.opensoutheners.com/oss/flex-url/advanced/typed-schemas.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
