To augment hapi
module to add type checking for server methods, following is the code:
import { Server } from 'hapi';
declare module 'hapi' {
export interface Server {
methods: {
getUsername: () => Promise<string>;
};
}
}
However, this doesn't work because @types/hapi
package already defines methods
as:
readonly methods: Util.Dictionary<ServerMethod>;
From this StackOverflow question, As far as I know, it is not possible to redefine the existing property. But I am not sure. I could not find any documentation about this. If this is not the case, how can I augment/patch/fix the type info?
As a workaround, I have created a new Hapi.js Server interface that extends the original interface. So instead of using hapi Server
interface, I am using the one I created:
// src/augment.d.ts
import { Server as HapiServer } from 'hapi';
export interface Server extends HapiServer {
methods: {
getUsername: () => Promise<string>;
};
}
// In my file src/server.ts file
import { Server } from 'src/augment';
// NOW IT TYPECHECKS CORRECTLY
const username = server.methods.getUsername();
Is this the right approach to take?