fastify / fastify-secure-session

Create a secure stateless cookie session for Fastify
MIT License
195 stars 45 forks source link

typescript types don't work properly #211

Closed Zamiell closed 5 months ago

Zamiell commented 7 months ago

Prerequisites

Fastify version

4.25.1

Plugin version

7.1.0

Node.js version

20.9.0

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

The types don't work properly. e.g.:

declare module "@fastify/secure-session" {
  interface SessionData {
    userID: number;
  }
}

async function login(
  request: FastifyRequest,
  reply: FastifyReply,
): Promise<FastifyReply> {
  request.session.set("usersID", 123);
}

In the above code snippet, we made a typo, and typed it as "usersID" instead of "userID". Oops! But we don't get a compiler error - it just works, and the bug makes its way to production.

Is there some way for a declaration merge to get rid of the string index signature? Because that's the root cause of the bug.

mcollina commented 6 months ago

Is there some way for a declaration merge to get rid of the string index signature? Because that's the root cause of the bug.

Unfortunately no. I'm not sure how to fix this at all.

Gehbt commented 5 months ago

I have 2 proposal with demo: A: When set a mission key, the value should always be type never. B: Remove indexed access type and ensured extends what type is defined.

commit A is base avoid any commit B is base remove the indexed access type

Unfortunately no. I'm not sure how to fix this at all.

mcollina commented 5 months ago

Would you like to send a Pull Request to address this issue? Remember to add unit tests.