hershmire / fastify-auth-ts-bug

Bug replication for https://github.com/fastify/fastify-auth/issues/207

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fastify Auth Typescript Bug

This repo is intended to reproduce a Typescript bug when using @fastify/auth. More info in this issue – fastify/fastify-auth#207.

Setup

Install dependencies:

yarn install

To see the Typescript error:

yarn types:check

By running the above, you will see the following error:

src/users-controller.ts:29:9 - error TS2322: Type '(request: FastifyRequest<{    Params: Partial<GetUserRequestParams>;}>, reply: FastifyReply) => Promise<void>' is not assignable to type 'FastifyAuthFunction | FastifyAuthFunction[]'.
  Type '(request: FastifyRequest<{    Params: Partial<GetUserRequestParams>;}>, reply: FastifyReply) => Promise<void>' is not assignable to type 'FastifyAuthFunction'.
    Types of parameters 'request' and 'request' are incompatible.
      Type 'FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>' is not assignable to type 'FastifyRequest<{ Params: Partial<{ userId: string; }>; }, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>'.
        Type 'RouteGenericInterface' is not assignable to type '{ Params: Partial<{ userId: string; }>; }'.
          Types of property 'Params' are incompatible.
            Type 'unknown' is not assignable to type 'Partial<{ userId: string; }>'.

29         usersMutationAccessPolicy(fastify),
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in src/users-controller.ts:29

Start the app:

yarn dev

Running a successful PATCH request:

curl -X PATCH http://localhost:8080/users/test-actor-id-123 -H 'Content-Type: application/json' -d '{ "firstName": "New Name" }'
# {"success":true}

Seeing the access policy force the PATCH to fail:

curl -X PATCH http://localhost:8080/users/bad-actor -H 'Content-Type: application/json' -d '{ "firstName": "New Name" }'
# {"statusCode":404,"code":"RESOURCE_NOT_FOUND","error":"Not Found","message":"Resource 'bad-actor' not found"}

About

Bug replication for https://github.com/fastify/fastify-auth/issues/207


Languages

Language:TypeScript 86.9%Language:JavaScript 13.1%