fastify / fastify/fastify-autoload

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

Open
#297 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
373
Forks
77
PR merge metrics
No merged PRs in 30d

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] 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:
```typescript
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:

```bash
server:dev: src/index.ts(22,32): error TS2339: Property 'conf' does not exist on type 'FastifyInstance, FastifyBaseLogger, FastifyTypeProviderDefault> & PromiseLike<...>'.
server:dev: src/index.ts(23,7): error TS2339: Property 'blipp' does not exist on type 'FastifyInstance, 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:

```typescript

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

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

export type Schema = Static;

const configKey = 'conf';
const config = envSchema({
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.