Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
irg1008 opened this issue Jan 22, 2023 · 1 comment
Open
2 tasks done

Comments

@irg1008
Copy link

irg1008 commented Jan 22, 2023

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.

@mcollina
Copy link
Member

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

This is correct. You'd need to import the modules at the top to use their typings. Otherwise, typescript won't know how to load them.

I don't know how to load the types automatically by @fastify/autoload.

Maybe @fastify/typescript knows how to implement this.


I don't understand the following sentence:

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

How does this relates to this module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants