Skip to content

Fix: Correct AsyncLocalStorage type parameter to match actual store - #222

Merged
Tony133 merged 3 commits into
fastify:mainfrom
gleb-lobastov:patch-1
Dec 3, 2026
Merged

Tony133 merged 3 commits into
fastify:mainfrom
gleb-lobastov:patch-1

Conversation

@gleb-lobastov

Copy link
Copy Markdown
Contributor

Fix: Correct AsyncLocalStorage type parameter to match actual store (RequestContextData)

The AsyncLocalStorage was incorrectly typed as RequestContext, but in reality it stores RequestContextData, which is directly accessed via key-value operations (e.g. store[key]).

Checklist

…RequestContextData)

The AsyncLocalStorage was incorrectly typed as RequestContext, but in reality it stores RequestContextData, which is directly accessed via key-value operations (e.g. store[key]).

Signed-off-by: Gleb <gleb-lobastov@users.noreply.github.com>

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@gurgunday gurgunday left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Fdawgs
Fdawgs requested a review from Copilot August 14, 2025 17:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a type inconsistency in the AsyncLocalStorage type parameter. The AsyncLocalStorage was incorrectly typed as storing RequestContext when it actually stores RequestContextData.

  • Corrects the AsyncLocalStorage type parameter from RequestContext to RequestContextData
  • Updates type test to properly validate the AsyncLocalStorage stores RequestContextData

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@tobiasdcl

Copy link
Copy Markdown

It would be great if this could be merged 🙌

I ran into this exact problem while following the Usage outside of a request as described in the readme:

import { asyncLocalStorage } from '@fastify/request-context';

it('should set request context', () => {
  asyncLocalStorage.run({}, async () => {
    requestContext.set('userId', 'some-fake-user-id');
    someCodeThatUsesRequestContext(); // requestContext.get('userId') will work
  })
})

results in TS error:

Argument of type '{}' is not assignable to parameter of type 'RequestContext'.
      Type '{}' is missing the following properties from type 'RequestContext': get, set, getStore

@Tony133

Tony133 commented Sep 3, 2026

Copy link
Copy Markdown
Member

@gleb-lobastov as soon as you have time, can you resolve the conflicts?

@jeremywiebe

Copy link
Copy Markdown

Ran into this today also.

# Conflicts:
#	types/index.d.ts
#	types/index.test-d.ts

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Tony133

Tony133 commented Dec 3, 2026

Copy link
Copy Markdown
Member

I've aligned this branch with main and resolved the conflicts, since the PR had been stale for a while:

  • types/index.d.ts: kept the current main version (deprecated exports already removed) and re-applied the type fix.
  • types/index.test-d.ts: removed, as type tests moved from tsd to tstyche in refactor(types): migrate from tsd to tstyche #243. The assertion was ported to types/index.tst.ts.

@Tony133

Tony133 commented Dec 3, 2026

Copy link
Copy Markdown
Member

I also typed the store as Partial<RequestContextData>: it starts empty and is filled later, so asyncLocalStorage.run({}, ...) now compiles even when RequestContextData declares required fields.

@Tony133
Tony133 merged commit 62bc50f into fastify:main Dec 3, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants