Skip to content

2.0.0 Beta 2

Pre-release
Pre-release

Choose a tag to compare

@fjorgemota fjorgemota released this 03 Aug 13:38
· 122 commits to main since this release
6d1a837

This release fixes two issues related to types in TypeScript:

  1. Now, we expect that a service factory will be passed to the factory() method. This allows us to maintain type information related to the container passed as argument to that function, so TypeScript basically can check out this code:
interface ServiceMap {
    database: DatabaseInstance;
    logger: LoggerInstance;
}
const container = Jimple.create<ServiceMap>();
container.set("database", container.factory( (c) => {
    // now, TypeScript will know that c is an instance of the container, so you can do 
    c.logger.log("Connecting to the database");
    return ...;
});
  1. The type information of the function passed to the protect() method is kept, so the function will be typed checked to what the container expects. So this works:
interface ServiceMap {
    urlValidator: (url: string) => boolean
}
const container = Jimple.create<ServiceMap>();
container.set("urlValidator", container.protect( (url: string) : boolean => {
    // If you change the above line to "url : number" instead of "url: string", it will throw a compile-time error
    return true;
});