Skip to content

fix: ajvFilePlugin type incompatible with Fastify's ajv.plugins option #605

@nahyeongjin1

Description

@nahyeongjin1

Bug Description

The ajvFilePlugin type definition is incompatible with Fastify's ajv.plugins option, causing TypeScript errors when registering the plugin.

Current Type

export function ajvFilePlugin (ajv: any): void

Expected Type

export function ajvFilePlugin (ajv: Ajv): Ajv

Steps to Reproduce

import fastify from 'fastify'
import { fastifyMultipart, ajvFilePlugin } from '@fastify/multipart'

const app = fastify({
  ajv: {
    plugins: [ajvFilePlugin]  // TypeScript error here
  }
})

app.register(fastifyMultipart)

Error Message

error TS2769: No overload matches this call.
  The last overload gave the following error.
    Type '(ajv: any) => void' is not assignable to type 'Plugin<unknown> | [Plugin<unknown>, unknown]'.
      Type '(ajv: any) => void' is not assignable to type 'Plugin<unknown>'.
        Type 'void' is not assignable to type 'Ajv'.

Root Cause

Fastify's ajv.plugins option expects Plugin<unknown> type from @fastify/ajv-compiler, which is defined as:

export interface Plugin<Opts> {
    (ajv: Ajv, options?: Opts): Ajv;  // Must return Ajv
    [prop: string]: any;
}

The actual implementation in index.js correctly returns Ajv:

function ajvFilePlugin (ajv) {
  return ajv.addKeyword({...})  // addKeyword returns Ajv
}

But the type definition incorrectly declares the return type as void.

Proposed Fix

  1. Add ajv to devDependencies for type definitions
  2. Update types/index.d.ts:
import type Ajv from 'ajv'

// ...

export function ajvFilePlugin (ajv: Ajv): Ajv

Environment

  • @fastify/multipart: 9.4.0
  • fastify: 5.7.4
  • TypeScript: 5.8.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions