diff --git a/src/interfaces/interfaces.ts b/src/interfaces/interfaces.ts index 025380ecd..f6af2483d 100644 --- a/src/interfaces/interfaces.ts +++ b/src/interfaces/interfaces.ts @@ -4,7 +4,11 @@ namespace interfaces { new (...args: any[]): T; } - export type ServiceIdentifier = (string | symbol | Newable); + export interface Abstract { + prototype: T; + } + + export type ServiceIdentifier = (string | symbol | Newable | Abstract); export interface Binding extends Clonable> { guid: string; diff --git a/test/bugs/bugs.test.ts b/test/bugs/bugs.test.ts index 63cae6fcd..3e883231d 100644 --- a/test/bugs/bugs.test.ts +++ b/test/bugs/bugs.test.ts @@ -6,7 +6,8 @@ import { injectable, named, inject, - interfaces + interfaces, + unmanaged } from "../../src/inversify"; describe("Bugs", () => { @@ -230,4 +231,49 @@ describe("Bugs", () => { }); + it("Should be able to use an abstract class as the serviceIdentifier", () => { + + @injectable() + abstract class Animal { + protected name: string; + constructor(@unmanaged() name: string) { + this.name = name; + } + public abstract makeSound(input: string): string; + public move(meters: number) { + return this.name + " moved " + meters + "m"; + } + } + + @injectable() + class Snake extends Animal { + constructor() { + super("Snake"); + } + public makeSound(input: string): string { + return "sssss" + input; + } + public move() { + return "Slithering... " + super.move(5); + } + } + + @injectable() + class Jungle { + public animal: Animal; + constructor(@inject(Animal) animal: Animal) { + this.animal = animal; + } + } + + let kernel = new Kernel(); + kernel.bind(Animal).to(Snake); + kernel.bind(Jungle).to(Jungle); + + let jungle = kernel.get(Jungle); + expect(jungle.animal.makeSound("zzz")).to.eql("ssssszzz"); + expect(jungle.animal.move(5)).to.eql("Slithering... Snake moved 5m"); + + }); + });