From 99f2fb9538879025f673f53ded5551cf51a9fe7f Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sat, 9 Jul 2022 18:19:41 +0300 Subject: [PATCH 1/6] Update TypeScript.md --- docs/Reference/TypeScript.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/Reference/TypeScript.md b/docs/Reference/TypeScript.md index 4b807aec4f..25fc1261b4 100644 --- a/docs/Reference/TypeScript.md +++ b/docs/Reference/TypeScript.md @@ -643,10 +643,11 @@ 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 +> Using `require` won't load the type definitions properly and may cause a type errors + +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 From fefd5d51c9e4144a26d2f87754c715207179c6b9 Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sat, 9 Jul 2022 21:58:26 +0300 Subject: [PATCH 2/6] Update TypeScript.md --- docs/Reference/TypeScript.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/docs/Reference/TypeScript.md b/docs/Reference/TypeScript.md index 25fc1261b4..a9b3ab0541 100644 --- a/docs/Reference/TypeScript.md +++ b/docs/Reference/TypeScript.md @@ -645,8 +645,6 @@ Inheritance](https://dev.to/ethanarrowood/is-declaration-merging-and-generic-inh 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. -> Using `require` won't load the type definitions properly and may cause a type errors - 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 @@ -662,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 +{ + "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. From 571ef0dee732cab04889d1e2bf6073d677ed52e6 Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sat, 9 Jul 2022 22:19:07 +0300 Subject: [PATCH 3/6] Update TypeScript.md --- docs/Reference/TypeScript.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/Reference/TypeScript.md b/docs/Reference/TypeScript.md index a9b3ab0541..41afe83f81 100644 --- a/docs/Reference/TypeScript.md +++ b/docs/Reference/TypeScript.md @@ -643,9 +643,10 @@ 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. +Import the plugin with `import/from` and you're all set -- except there is one +exception users should be aware of. -One exception users should be aware of is that Fastify plugins use declaration merging to modify existing Fastify type +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 @@ -660,7 +661,10 @@ 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: +Note that using `require` won't 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. From 567e5de01f1852bee214fd60f55fce96c708f298 Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sun, 10 Jul 2022 19:33:26 +0300 Subject: [PATCH 4/6] Update docs/Reference/TypeScript.md Co-authored-by: Frazer Smith --- docs/Reference/TypeScript.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Reference/TypeScript.md b/docs/Reference/TypeScript.md index 41afe83f81..11583a64d4 100644 --- a/docs/Reference/TypeScript.md +++ b/docs/Reference/TypeScript.md @@ -661,7 +661,7 @@ 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 +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: From 45f064a4701c4161f1557c0716b2a49c9a4249b4 Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sun, 10 Jul 2022 19:33:34 +0300 Subject: [PATCH 5/6] Update docs/Reference/TypeScript.md Co-authored-by: Frazer Smith --- docs/Reference/TypeScript.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Reference/TypeScript.md b/docs/Reference/TypeScript.md index 11583a64d4..5509650ad8 100644 --- a/docs/Reference/TypeScript.md +++ b/docs/Reference/TypeScript.md @@ -663,8 +663,8 @@ However, there are a couple of suggestions to help improve this experience: 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 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. From 6929ad74e5346ad37dfdd063ad2c1f1003d8ab44 Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sun, 10 Jul 2022 19:33:45 +0300 Subject: [PATCH 6/6] Update docs/Reference/TypeScript.md Co-authored-by: Frazer Smith --- docs/Reference/TypeScript.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Reference/TypeScript.md b/docs/Reference/TypeScript.md index 5509650ad8..9143de1c6e 100644 --- a/docs/Reference/TypeScript.md +++ b/docs/Reference/TypeScript.md @@ -679,7 +679,7 @@ fastify.register(plugin) ``` Or even explicit config on tsconfig -```json +```jsonc { "types": ["plugin"] // we force TypeScript to import the types }