Last active
June 23, 2022 02:19
-
-
Save casheeeewnuts/29d022ab6ad94f35ec9c2fb876247874 to your computer and use it in GitHub Desktop.
tsyringeの@Injectableの挙動
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "reflect-metadata" | |
import { container, inject, injectable } from "tsyringe" | |
@injectable() | |
class A { | |
constructor() { | |
console.log("class A instantiated!") | |
} | |
} | |
@injectable() | |
class B { | |
constructor( | |
@inject(A) | |
private a: A | |
) { | |
console.log("class B instantiated!") | |
} | |
} | |
container.resolve(A) | |
container.resolve(B) | |
// console.log outputs | |
// class A instantiated! | |
// class A instantiated! | |
// class B instantiated! | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "reflect-metadata" | |
import { container, inject, injectable, singleton } from "tsyringe" | |
@injectable() | |
class A { | |
constructor() { | |
console.log("class A instantiated!") | |
} | |
} | |
@singleton() | |
class C { | |
constructor() { | |
console.log("class C instantiated!") | |
} | |
} | |
@injectable() | |
class B { | |
constructor( | |
@inject(A) | |
private a: A, | |
@inject(C) | |
private c: C | |
) { | |
console.log("class B instantiated!") | |
} | |
} | |
container.resolve(A) | |
container.resolve(C) | |
container.resolve(B) | |
// console.log outputs | |
// class A instantiated! | |
// class C instantiated! | |
// class A instantiated! | |
// class B instantiated! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment