Skip to content

Instantly share code, notes, and snippets.

@Nixes
Last active December 19, 2024 04:45
Show Gist options
  • Save Nixes/bd3c9c6b7a7bbfd29786c51246d680d2 to your computer and use it in GitHub Desktop.
Save Nixes/bd3c9c6b7a7bbfd29786c51246d680d2 to your computer and use it in GitHub Desktop.
Example async injectable design for DI in typescript
// 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