Last active
December 19, 2024 04:45
-
-
Save Nixes/bd3c9c6b7a7bbfd29786c51246d680d2 to your computer and use it in GitHub Desktop.
Example async injectable design for DI in typescript
This file contains hidden or 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
// unfortunately typescript does not have support for static member checking in interface, if this changes create an interface for this | |
export default class TestAsyncInjectableService { | |
private constructor( | |
private injectedString: string | |
) {} | |
public static async construct(injectedString: string): Promise<TestAsyncInjectableService> { | |
const service = new TestAsyncInjectableService(injectedString); | |
await service.doSomethingAsync(); | |
return service; | |
} | |
} | |
// example usage | |
const injectedString = ""; | |
const injectedService = await TestAsyncInjectableService.construct(injectedString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment