diff --git a/types/types.d.ts b/types/types.d.ts index 1c504f7..e7483c7 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -88,10 +88,10 @@ declare namespace fastifySession { save(): Promise; /** sets values in the session. */ - set(key: K, value: V): void; + set(key: K, value: Fastify.Session[K]): void; /** gets values from the session. */ - get(key: K): V; + get(key: K): Fastify.Session[K] | undefined; /** checks if session has been modified since it was generated or loaded from the store. */ isModified(): boolean; diff --git a/types/types.test-d.ts b/types/types.test-d.ts index a37e32f..e848020 100644 --- a/types/types.test-d.ts +++ b/types/types.test-d.ts @@ -113,7 +113,7 @@ app.route({ expectType<{ id: number } | undefined>(session?.user); }); expectType(request.session.set('foo', 'bar')); - expectType(request.session.get<'foo', string>('foo')); + expectType(request.session.get('foo')); expectType(request.session.touch()); expectType(request.session.isModified()); expectType(request.session.reload(() => {})); @@ -144,12 +144,18 @@ const app2 = fastify() app2.register(fastifySession) app2.get('/', async function(request) { + let num: number | undefined, str: string | undefined; + expectError(num = request.session.get('foo')); + expectAssignable(str = request.session.get('foo')); + expectError(request.session.set('foo', 2)); + expectAssignable(request.session.set('foo', 'bar')); + expectType(request.session.get('user')) expectAssignable(request.session.set('user', { id: 2 })) expectError(request.session.get('not exist')) expectError(request.session.set('not exist', 'abc')) - expectType<'bar'>(request.session.get('not exist')) - expectAssignable(request.session.set('not exist', 'abc')) + expectType(request.session.get('not exist')) + expectAssignable(request.session.set('not exist', 'abc')) }) \ No newline at end of file