TypeScript: How to customize types definition of bottle.container ?
Toilal opened this issue · comments
I'm using bottlejs with TypeScript, but I need to write this anoying stuff to make TypeScript compiler happy.
// I don't use TypeScript to finally write this :(
(bottle.container as any).MapCache as MapCache;
It seems to be possible to extend typings definitions with declarations merge features, but I can't make it work.
In typings.d.ts
, I have other declarations, and when I try to write this one:
declare module Bottle {
import MapCAche from "./utils/mapcache"; // This is a .ts module, not a .d.ts definition file
interface IContainer {
MapCache: MapCache;
}
}
It gives a compiler error : TS1147:Import declarations in a namespace cannot reference a module
.
Any idea to make it work ? How to add types definition on the container ?
I read the TypeScript doc page about declarations merge until the end, and I found the answer in the last section :). This example just work as expected :
import Bottle from "bottlejs";
import MapCache from "./utils/mapcache";
let bottle = new Bottle();
bottle.service("MapCache", () => MapCache);
export default bottle;
declare module "bottlejs" { // Use the same module name as the import string
interface IContainer {
MapCache: MapCache;
}
}
Is there a way this could work with dynamic named properties?
We define our services like bottle.service(EventService.name, EventService)
, and retrieve it by using the name property again: container[EventService.name]
.
But how would you define that in the interface?