Skip to content

Property '<property>' does not exist on type FastifyInstance when loading TS plugin #297

Open
@irg1008

Description

@irg1008

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.12.0

Plugin version

5.7.1

Node.js version

18.12.1

Operating system

Windows

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

11

Description

I have a fastify TS project and I use autoload to load both plugins and routes automatically.

This is my app.ts:

const start = async () => {
  const app = fastify(fastifyOptions);

  // This autoload function has the problem of not able to load the typescript namespace declaration, so when compiling error is risen.
  await app.register(autoLoad, {
    dir: path.join(__dirname, 'plugins'),
  });

  app.register(autoLoad, {
    dir: path.join(__dirname, 'routes'),
  });

  app.get('/', async () => {
    return { hello: 'world' };
  });

  await app.listen({ port: app.conf.PORT });
  app.blipp();
};

start();

I run the app in dev mode with the next command:

ts-node-dev ./src/index.ts

The problem is that upon start typescript complains of the following error:

server:dev: src/index.ts(22,32): error TS2339: Property 'conf' does not exist on type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault> & PromiseLike<...>'.
server:dev: src/index.ts(23,7): error TS2339: Property 'blipp' does not exist on type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault> & PromiseLike<...>'

This errors only happen when having "--transpile-only" ts-node option disabled.

Also, removing autoload plugin and importing the plugins individually works as well (without the transpile-only option).

An example of how fastify module is extended can be seen in the following snippet:

declare module 'fastify' {
  export interface FastifyInstance {
    [configKey]: Schema;
  }
}

const schema = Type.Object({
  PORT: Type.Number({ default: 3000 }),
});

export type Schema = Static<typeof schema>;

const configKey = 'conf';
const config = envSchema<Schema>({
  schema,
  dotenv: true,
});

export default fastifyPlugin(async (app) => {
  app.decorate(configKey, config);
});

Important to note than plugin downloaded from packages with types declaration DO NOT work either.

Steps to Reproduce

  1. Create a TS fastify project
  2. Use autload to import typed plugins
  3. Try to use the extended property for FastifyInstance
  4. TS complains that the interface does not contain said property

(It works without transpiling or by importing plugins "by-hand")

Expected Behavior

FastifyInstance interface should be extended with new declared properties upon loading the plugins.

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