fastify/fastify-passport

passport types not exists when using ESM + Typescript

rluvaton opened this issue · 1 comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.15.0

Plugin version

2.2.0

Node.js version

18

Operating system

macOS

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

13.2.1

Description

When using ESM with TypeScript the types are missing.

Example with this code:

import fastifyPassport from '@fastify/passport';

fastifyPassport.initialize()
fastifyPassport.secureSession()

fastifyPassport.use(undefined as any, undefined as any);

I get:

$ tsc --notEmit
src/index.ts:3:17 - error TS2339: Property 'initialize' does not exist on type 'typeof import("<repo-path>/node_modules/@fastify/passport/dist/index")'.

3 fastifyPassport.initialize()
                  ~~~~~~~~~~

src/index.ts:4:17 - error TS2339: Property 'secureSession' does not exist on type 'typeof import("<repo-path>/node_modules/@fastify/passport/dist/index")'.

4 fastifyPassport.secureSession()
                  ~~~~~~~~~~~~~

src/index.ts:6:17 - error TS2339: Property 'use' does not exist on type 'typeof import("<repo-path>/node_modules/@fastify/passport/dist/index")'.

6 fastifyPassport.use(undefined as any, undefined as any);
                  ~~~


Found 3 errors in the same file, starting at: src/index.ts:3

Steps to Reproduce

Repro

or:

  1. run:
npm init -y

npm i typescript fastify @fastify/passport @types/node

./node_modules/.bin/tsc --init
  1. add this to src/index.ts file:
import fastifyPassport from '@fastify/passport';

fastifyPassport.initialize()
fastifyPassport.secureSession()

fastifyPassport.use(undefined as any, undefined as any);
  1. Add "type": "module" in package.json
  2. in tsconfig.json replace "module": "commonjs" with "module": "Node16"
  3. run tsc

Expected Behavior

should compile without errors

My bad, I also needed to add "moduleResolution": "Node" to the tsconfig compilerOptions

{
  // ...
  "compilerOptions": {
     //  ... 
     "moduleResolution": "Node",
     "module": "Node16",
   }
}