fastify / point-of-view

Template rendering plugin for Fastify
MIT License
338 stars 86 forks source link

Typedefs don't support "view" method in FastifyInstance #341

Closed pr0n1x2 closed 1 year ago

pr0n1x2 commented 2 years ago

Prerequisites

Fastify version

4.4.0

Plugin version

No response

Node.js version

v14.15.1

Operating system

Windows

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

10

Description

The documentation says the following: The fastify object is decorated the same way as reply and allows you to just render a view into a variable instead of sending the result back to the browser:

But this is not described in the index.d.ts file, which is why the typescript compiler does not see the "view" method in FastifyInstance interface.

Solution: Extend FastifyInstance interface in index.d.ts

import { FastifyPlugin, FastifyReply, FastifyInstance, RawServerBase } from 'fastify';

declare module "fastify" {

  interface RouteSpecificOptions {
    layout?: string;
  }

  interface FastifyReply {
    view<T extends { [key: string]: any; }>(page: string, data: T, opts?: RouteSpecificOptions): FastifyReply;
    view(page: string, data?: object, opts?: RouteSpecificOptions): FastifyReply;
  }

  interface FastifyInstance {
    view<T extends { [key: string]: any; }, R>(page: string, data: T, opts?: RouteSpecificOptions): Promise<R>;
    view<R>(page: string, data?: object, opts?: RouteSpecificOptions): Promise<R>;
  }
}

No response