DEV Community

Discussion on: minimal DI container in TypeScript

Collapse
 
mindplay profile image
Rasmus Schultz • Edited

It's checking the keys - so far so good:

function Container<P extends { [name: string]: { (c: any): Promise<any> } }>(provider: P) {
    const cache: { [name in keyof P]?: Promise<any> } = {};

    const container = function(name: keyof P) {
        if (!cache[name]) {
            cache[name] = provider[name](container);
        }

        return cache[name];
    }

    return container;
}

Now to figure out the generic return-type...