Skip to content

Commit

Permalink
Allow abstract classes to be used as identifiers #363
Browse files Browse the repository at this point in the history
  • Loading branch information
remojansen committed Sep 10, 2016
1 parent 6041cd4 commit 38b9a8a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ namespace interfaces {
new (...args: any[]): T;
}

export type ServiceIdentifier<T> = (string | symbol | Newable<T>);
export interface Abstract<T> {
prototype: T;
}

export type ServiceIdentifier<T> = (string | symbol | Newable<T> | Abstract<T>);

export interface Binding<T> extends Clonable<Binding<T>> {
guid: string;
Expand Down
48 changes: 47 additions & 1 deletion test/bugs/bugs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
injectable,
named,
inject,
interfaces
interfaces,
unmanaged
} from "../../src/inversify";

describe("Bugs", () => {
Expand Down Expand Up @@ -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>(Animal).to(Snake);
kernel.bind<Jungle>(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");

});

});

0 comments on commit 38b9a8a

Please sign in to comment.