Skip to content

docs: fix TypeScript inconsistency in Decorators.md #6224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: resolve markdown linting issues
- Fix line length violations in docs/Reference/Decorators.md
- Fix line length violations in docs/Reference/TypeScript.md
- All lines now comply with 80 character limit
- Addresses linting feedback from PR review
  • Loading branch information
emicovi committed Jun 19, 2025
commit a66ca6d7523f900b95f9f923e89deed7d98fdc94
15 changes: 10 additions & 5 deletions docs/Reference/Decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ const helper = reply.getDecorator('helper')

The `getDecorator` method is useful for:

- **Plugin dependency validation**: Verify that required decorators exist at registration time
- **Runtime safety**: Ensure decorators exist before using them, avoiding undefined access
- **Plugin dependency validation**: Verify that required decorators exist at
registration time
- **Runtime safety**: Ensure decorators exist before using them, avoiding
undefined access
- **Plugin encapsulation**: Access decorators within specific plugin contexts

```js
Expand All @@ -403,7 +405,8 @@ fastify.register(async function (fastify) {
```

> **Note**: For TypeScript users, `getDecorator` supports generic type parameters.
> See the [TypeScript documentation](/docs/latest/Reference/TypeScript/) for advanced typing examples.
> See the [TypeScript documentation](/docs/latest/Reference/TypeScript/) for
> advanced typing examples.

#### `setDecorator(name, value)`
<a id="set-decorator"></a>
Expand Down Expand Up @@ -436,5 +439,7 @@ fastify.addHook('preHandler', async (req, reply) => {
})
```

> **Note**: For TypeScript users, `setDecorator` supports generic type parameters for enhanced type safety.
> See the [TypeScript documentation](/docs/latest/Reference/TypeScript/) for advanced typing examples.
> **Note**: For TypeScript users, `setDecorator` supports generic type parameters
> for enhanced type safety.
> See the [TypeScript documentation](/docs/latest/Reference/TypeScript/) for
> advanced typing examples.
20 changes: 14 additions & 6 deletions docs/Reference/TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,10 @@ Or even explicit config on tsconfig

### Decorators

Fastify's decorator system allows you to extend the Fastify instance, request, and reply objects. When using TypeScript, decorators provide additional type safety and advanced features beyond the basic JavaScript usage documented in the [Decorators reference](./Decorators.md).
Fastify's decorator system allows you to extend the Fastify instance, request, and
reply objects. When using TypeScript, decorators provide additional type safety and
advanced features beyond the basic JavaScript usage documented in the
[Decorators reference](./Decorators.md).

#### `getDecorator<T>` with Type Parameters

Expand Down Expand Up @@ -720,7 +723,8 @@ declare module 'fastify' {
}
```

This approach modifies the Fastify instance globally, which may lead to conflicts and inconsistent behavior in multi-server setups or with plugin encapsulation.
This approach modifies the Fastify instance globally, which may lead to conflicts
and inconsistent behavior in multi-server setups or with plugin encapsulation.

Using `getDecorator<T>` allows you to limit types scope:

Expand Down Expand Up @@ -769,7 +773,8 @@ function sendSuccess (this: FastifyReply) {
export type SendSuccess = typeof sendSuccess
```

However, `getDecorator` returns functions with the `this` context already **bound**, meaning the `this` parameter disappears from the function signature.
However, `getDecorator` returns functions with the `this` context already **bound**,
meaning the `this` parameter disappears from the function signature.

To correctly type it, you should use `OmitThisParameter` utility:

Expand All @@ -789,7 +794,8 @@ fastify.get('/success', async (request, reply) => {

#### `setDecorator<T>` with Type Parameters

The `setDecorator<T>` method provides enhanced type safety for updating request decorators:
The `setDecorator<T>` method provides enhanced type safety for updating request
decorators:

```typescript
fastify.decorateRequest('user', '')
Expand All @@ -801,15 +807,17 @@ fastify.addHook('preHandler', async (req, reply) => {

**Type Safety Benefits**

If the `FastifyRequest` interface does not declare the decorator, you would typically need to use type assertions:
If the `FastifyRequest` interface does not declare the decorator, you would
typically need to use type assertions:

```typescript
fastify.addHook('preHandler', async (req, reply) => {
(req as typeof req & { user: string }).user = 'Bob Dylan'
})
```

The `setDecorator<T>` method eliminates the need for explicit type assertions while providing type safety:
The `setDecorator<T>` method eliminates the need for explicit type assertions
while providing type safety:

```typescript
fastify.addHook('preHandler', async (req, reply) => {
Expand Down