Skip to content
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

feat: add support session typing #13320

Merged
merged 4 commits into from
Feb 28, 2025
Merged

feat: add support session typing #13320

merged 4 commits into from
Feb 28, 2025

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Feb 26, 2025

Changes

Adds support for typing experimental session data. This works similarly to typing of Astro.locals, using src/env.d.ts to define ambient types that extend App.SessionData. For example:

declare namespace App {
  interface SessionData {
    user: {
      id: string;
      email: string;
    };
    lastLogin: Date;
  }
}

Any keys not defined in this interface will be treated as any.

Then when you access Astro.session in your components, any defined keys will be typed correctly:

---
const user = await Astro.session.get('user');
//    ^? const: user: { id: string; email: string; } | undefined

const something = await Astro.session.get('something');
//    ^? const: something: any

Astro.session.set('user', 1);
//    ^? Argument of type 'number' is not assignable to parameter of type '{ id: string; email: string; }'.
---

Testing

Tested manually
image

Docs

Docs PR: withastro/docs#11068
RFC update to follow

Copy link

changeset-bot bot commented Feb 26, 2025

🦋 Changeset detected

Latest commit: 39a3048

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) docs pr labels Feb 26, 2025
Copy link

codspeed-hq bot commented Feb 26, 2025

CodSpeed Performance Report

Merging #13320 will not alter performance

Comparing session-types (39a3048) with main (5f72a58)

Summary

✅ 6 untouched benchmarks

@ascorbic
Copy link
Contributor Author

This is to be merged after the 5.4 release

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Blocking to avoid merge

Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

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

Could it be possible to add types tests under test/types?

@ascorbic
Copy link
Contributor Author

@florian-lefebvre done

Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

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

Looks like you have unused imports in the test, but apart from that looks good!

@ascorbic ascorbic self-assigned this Feb 28, 2025
@ascorbic ascorbic merged commit b5dabe9 into main Feb 28, 2025
17 checks passed
@ascorbic ascorbic deleted the session-types branch February 28, 2025 15:32
@astrobot-houston astrobot-houston mentioned this pull request Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs pr pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants