From d45bcb6b3a3cb412a7b7799a34fe34867f1df854 Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Mon, 11 Jul 2022 10:39:33 +0300 Subject: [PATCH] Update TypeScript.md (#4126) * Update TypeScript.md * Update TypeScript.md * Update TypeScript.md * Update docs/Reference/TypeScript.md Co-authored-by: Frazer Smith * Update docs/Reference/TypeScript.md Co-authored-by: Frazer Smith * Update docs/Reference/TypeScript.md Co-authored-by: Frazer Smith Co-authored-by: Frazer Smith --- docs/Reference/TypeScript.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/Reference/TypeScript.md b/docs/Reference/TypeScript.md index 4b807aec4f..9143de1c6e 100644 --- a/docs/Reference/TypeScript.md +++ b/docs/Reference/TypeScript.md @@ -661,6 +661,30 @@ 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` will not load the type definitions properly and may +cause type errors. +TypeScript can only identify the types that are directly imported into 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 +```jsonc +{ + "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.