unjs / h3

⚡️ Minimal H(TTP) framework built for high performance and portability
https://h3.unjs.io/
MIT License
3.69k stars 218 forks source link

Banging my head on a wall: How to add types to session object? #871

Closed ultimateshadsform closed 2 months ago

ultimateshadsform commented 2 months ago

Describe the change

I've been sitting all day trying to add my own types to the SessionDataT object. I don't like using any type. Is there a way to do this in TypeScript?

My session.d.ts:

import { type Session } from 'h3';
import { IUser } from '@/types/user.ts';

interface Data {
  user: IUser;
}

declare module 'h3' {
  export interface Session {
    Data: Data;
  }
}

But it doesn't seem to work. I also don't want to do (session.data as Usertype) etc I just want to do a d.ts like you can with express.

I'm also using Nuxt 3 using h3.

ultimateshadsform commented 2 months ago

Nvm. Should add this to the docs. But you do it like this:

useSession<your type>