I have a Node application that depends on @types/hapi
. I'd like to add a property to one of the classes defined in this module. I've tried defining my new property via module augmentation:
// my-custom-hapi-typings.d.ts
import * as hapi from 'hapi';
declare module hapi {
interface Server {
myProperty: string;
}
}
This doesn't cause any compiler errors, but it also doesn't add the myProperty
property to the Server
class. I still get errors elsewhere in my project when I try and reference the property like this:
// my-other-file.ts
import * as hapi from 'hapi';
const server = new hapi.Server({ ... });
// error: Property 'myProperty' does not exist on type 'Server'.
server.myProperty = 'hello';
Why does the TypeScript compiler seem to be ignoring my .d.ts
file? Do I need to "import" the file or somehow make the TypeScript compiler aware that this file exists? I was under the impression that just placing a .d.ts
file in the source directory was enough for TypeScript to pick up on these augmentations.
import
makes the file a module. Try removing it. – Athabaskaimport
and the result is the same. – Catalandeclare module "hapi"
. You might also need toexport
theServer
interface. – Athabaska'hapi'
in quotes fixed the issue! Thanks! Not sure I understand why though. If you want to add your comment as an answer, I'll accept. – Catalan