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

Update TypeScript.md #4126

Merged
merged 6 commits into from Jul 11, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions docs/Reference/TypeScript.md
Expand Up @@ -643,10 +643,9 @@ Inheritance](https://dev.to/ethanarrowood/is-declaration-merging-and-generic-inh
#### Using a Plugin

Using a Fastify plugin in TypeScript is just as easy as using one in JavaScript.
Import the plugin with `import/from` and you're all set -- except there is one
exception users should be aware of.
Import the plugin with `import/from` and you're all set.

Fastify plugins use declaration merging to modify existing Fastify type
One exception users should be aware of is that Fastify plugins use declaration merging to modify existing Fastify type
interfaces (check out the previous two examples for more details). Declaration
merging is not very _smart_, meaning if the plugin type definition for a plugin
is within the scope of the TypeScript interpreter, then the plugin types will be
Expand All @@ -661,6 +660,27 @@ However, there are a couple of suggestions to help improve this experience:
[npm-check](https://www.npmjs.com/package/npm-check) to verify plugin
dependencies are being used somewhere in your project.

Note that using `require` won't load the type definitions properly and may cause type errors. TypeScript can only identify the types that are direct imported in code which means that you can use require inline with import on top, for example:

```typescript
import 'plugin' // here will trigger the type augmentation.

fastify.register(require('plugin'))
```

```typescript
import plugin from 'plugin' // here will trigger the type augmentation.

fastify.register(plugin)
```

Or even explicit config on tsconfig
```json
mikicho marked this conversation as resolved.
Show resolved Hide resolved
{
"types": ["plugin"] // we force TypeScript to import the types
}
```

## Code Completion In Vanilla JavaScript

Vanilla JavaScript can use the published types to provide code completion (e.g.
Expand Down