fastify-cors icon indicating copy to clipboard operation
fastify-cors copied to clipboard

Incorrect default import types in ESM

Open SuperchupuDev opened this issue 3 years ago • 1 comments

Prerequisites

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

Fastify version

4.6.0

Plugin version

8.1.0

Node.js version

18.7.0

Operating system

Windows

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

10

Description

When importing @fastify/cors as ESM, TypeScript has the incorrect type definions for the default import, forcing the user to add .default to the import, even though just using the default import (cors in the case) works at runtime

image

image

I believe this is a typing issue, since the runtime works perfectly in each case

Steps to Reproduce

  • Have a TypeScript project with the following configuration:
    {
      "compilerOptions": {
        "target": "es2022",
        "lib": ["es2022"],
    
        "module": "esnext",
        "moduleResolution": "nodenext",
        "baseUrl": "src",
        "resolveJsonModule": true,
    
        "sourceMap": true,
        "outDir": "dist",
        "pretty": true,
        "noEmitOnError": true,
    
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
    
        "strict": true,
    
        "skipLibCheck": true
      }
    }
    
  • Import @fastify/cors
  • Try to register it normally
  • Run the TypeScript compiler

Expected Behavior

Assuming the plugin was imported as cors, using fastify.register(cors, ...) should compile successfully, it throws an error instead

SuperchupuDev avatar Sep 22 '22 18:09 SuperchupuDev

I have a fear this will fall under https://github.com/fastify/fastify/pull/4247.

mcollina avatar Sep 23 '22 08:09 mcollina

Got same error with Typescript 4.8.x for all fastify plugin (@fastify/*). But no error with Typescript 4.7.x.

Workaround:

import type { FastifyPluginCallback } from "fastify";
import type { FastifyCorsOptions } from '@fastify/cors';
await server.register(cors as unknown as FastifyPluginCallback<FastifyCorsOptions>, { });

No problem with @fastify/static plugin.

jagu-sayan avatar Oct 21 '22 08:10 jagu-sayan