Skip to content

Constructor parameters not being injected #10

Description

@Shawboy278

Hello,

I've been using your example of abstract classes and I'm experincing what seems like constructor parameters not being injected/passed along. The following is a modified version of the example you have posted. I'm inexperienced with Deno/Typescript enough to be uncertain if this is a bug or something I'm misunderstanding of how the language works.

Thank you for any insight you can provide.

import "https://cdn.skypack.dev/@abraham/reflection@0.8.0";
import { assert } from "https://deno.land/std@0.103.0/testing/asserts.ts";
import { Service, ServiceCollection } from "https://deno.land/x/di/mod.ts";

abstract class IA {
    abstract foo(): string;
}

abstract class IB {
    abstract IndirectFoo(): string;
    abstract bar(): string;
}

@Service()
class A implements IA {
    public foo(): string {
        return "foo";
    }
}

@Service()
class B implements IB {
    constructor(
        private a: IA
    ) {}

    public IndirectFoo(): string {
        return this.a.foo();
    }

    public bar(): string {
        return "bar";
    }
}

const serviceCollection = new ServiceCollection();
serviceCollection.addTransient(IA, A);
serviceCollection.addTransient(IB, B);

const ib = serviceCollection.get(IB);

Deno.test("Is expected type", () => {
    assert(ib instanceof B);
});

Deno.test("Expects 'bar'", () => {
    assert(ib.bar() == "bar");
});

Deno.test("Expects 'foo'", () => {
    assert(ib.IndirectFoo() == "foo");
});

Error message:

Check file:///Users/jason/Code/example/example.ts
running 3 tests from file:///Users/jason/Code/example/example.ts
test Is expected type ... ok (6ms)
test Expects 'bar' ... ok (4ms)
test Expects 'foo' ... FAILED (9ms)

failures:

Expects 'foo'
TypeError: Cannot read property 'foo' of undefined
    at B.IndirectFoo (file:///Users/jason/Code/example/example.ts:28:23)
    at file:///Users/jason/Code/example/example.ts:51:15
    at asyncOpSanitizer (deno:runtime/js/40_testing.js:35:15)
    at resourceSanitizer (deno:runtime/js/40_testing.js:72:13)
    at exitSanitizer (deno:runtime/js/40_testing.js:99:15)
    at runTest (deno:runtime/js/40_testing.js:215:13)
    at Object.runTests (deno:runtime/js/40_testing.js:283:28)
    at async file:///Users/jason/Code/example/832dba8c-7c32-4f9d-a6da-f1050ff07807$deno$test.js:4:7

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions