Skip to content

Removal of declaration merging #5061

@mcollina

Description

@mcollina

declaration merging is the technique we use in Fastify to decorate the types of our base objects. See https://github.com/fastify/fastify-static/blob/d40ce2d33481da7abcd261b1e8699ef31a9e2854/types/index.d.ts#L8-L17.

This technique has a major flaw: once a module is "seen" by TypeScript, those types are mergerd in original definition, even if the decorating plugin is not loaded in the current plugin tree.

After studying a bit of TypeScript, I think we can do better. In essence, we should be able to support something like:

import fp from 'fastify-plugin'
import { FastifyStaticMixin } from '@fastify/static'

export default fp(async function (app: FastifyInstance & FastifyStaticMixin, opts: object) {

})

Or even better:

import fp from 'fastify-plugin'
import { FastifyStaticMixin } from '@fastify/static'

export default fp(async function (app: FastifyInstance & FastifyStaticMixin, opts: object) {

}, {
  decorators: {
    reply: FastifyStaticMixin
  }
})

Or some other analog syntax with native Fastify, e.g. withMixin<T>().

I think this would be a significant improvement on our TS story for v5. Wdyt?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions