@types/xxxx package and had problems.Definitions by: in index.d.ts) so they can respond.Hello @jasonswearingen, @andy-ms, @AJamesPhillips, @SimonSchick,
I like hapi.js and I am starting to use typescript for typings, therefore we were extremely happy to find out that typings for hapi are already there and they are very good. Thank you for your OSS work!
I've one question though, regarding the server.plugins, maybe you also encountered this issue:

So server.plugins has type of PluginStates, which is an object with any keys and content.
As you know, in hapijs it's common that plugins which you are using in your project they are exposing some functionality, so do my plugins.
Can you, please, share me your thoughts - is there a way to redeclare plugins type, so it corresponds my project plugins list and the actual exposed interfaces that they do expose?
Let's say in my project I have plugin called myplugin, which exposes object with thy type MyType.
How can I make sure, that server.plugins.myplugin is resolved to the MyType type? I feel that I should be able to do either through extending Server or PluginsStates definition.
Ideally this additional declaration should take place inside the plugin folder, so each plugin extends something and adds additional (per project) definitions to the general definition.
I hope you can share more light on this topic for me :) I would appreciate any of your thoughts on this topic.
Once again, thank you in advance for your thoughts and thank you for contributing to the hapijs typings.
Regards,
Hi @PavelPolyakov yeah this is something I debated introducing when I rewrote the typings but felt it would be too much of a breaking change from existing typings. Can you do something like:
interface MyPluginStates {
myplugin: MyType;
}
Then in on every access of server.plugins use:
(server.plugins as MyPluginStates).myplugin ...
?
@AJamesPhillips
Thanks for the suggestion.
Do I understand right, that I'd need to explicitly import MyPluginStates in every file I use plugins?
This will work, but doesn't look like robust solution.
Do you know if it's possible to use this feature:
https://www.typescriptlang.org/docs/handbook/declaration-merging.html
and just merge over my declaration over the default PluginStates one?
@PavelPolyakov yes that's what I was recommending. And yes, it's not robust :(
There is declaration merging else where: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/hapi/test/server/decorate.ts#L62-L68
The current typings aren't set up for it though... or at least I'm pretty sure this approach wouldn't work. But please try it out and see.
hi @AJamesPhillips,
I've tried something inspired with your link and it seems it can work. I would like you to validate the solution, if possible.
So here is my tsconfig.json (attention to the paths section):
{
"compilerOptions": {
"baseUrl": ".",
"lib": [
"dom",
"es2016"
],
"paths": {
"*": [
"@types/*",
"src/*"
]
},
"allowJs": true,
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true
},
"include": [
"./src/**/*.ts"
],
"exclude": [
"node_modules",
"./src/**/__tests__/*.ts"
]
}
I've created index.d.ts in @types/hapi/ folder, with the next content:
import * as DTHapi from '@types/hapi';
export * from '@types/hapi';
export class Server extends DTHapi.Server {
some_server_method(arg1: number): string;
nono(wow:string, liam:boolean): boolean;
plugins: {
db: {
wow: boolean
}
}
}
Here is the IDE:


So the idea is to be able to extend the default definitely typings. I think conceptually it can be better than our initial suggestion which was discussed here. As if one is able to extend the definitions - it's possible to gather them in one file and in the project use default Server type.
What do you think? Glad if you have any suggestions regarding this solution.
Regards,
Hey @PavelPolyakov , thanks for exploring this. I'm not sure at the moment. This is not an area I'm an expert on yet. Will try to take a look sometime this week (though an ill at the moment so might be next week). Please prod if myself or anyone else hasn't got back to you by 23rd. Cheers.
Thanks for the help @AJamesPhillips, hope you are filling better!
Wanted to share, that thanks to @mhegazy I think I've found the most correct solution. Here it is:
https://github.com/PavelPolyakov/hapi-ts-vs-plugins-example
It is already possible to extend the default typings with augmentation!
Closing the issue then, hope it will help others.
Most helpful comment
Thanks for the help @AJamesPhillips, hope you are filling better!
Wanted to share, that thanks to @mhegazy I think I've found the most correct solution. Here it is:
https://github.com/PavelPolyakov/hapi-ts-vs-plugins-example
It is already possible to extend the default typings with augmentation!
Closing the issue then, hope it will help others.