Inject and injectable in ts

673 views Asked by At

I don't know much of OOPS pattern.

I have previously worked in react and never came to a point where Dependency injection and inversion are required (or anyone using it).

I was going through Theia Ide extension docs and was having tough time understanding this snippet

export const HelloWorldCommand = {
    id: 'HelloWorld.command',
    label: "Shows a message"
};

@injectable()
export class HelloWorldCommandContribution implements CommandContribution {

    constructor(
        @inject(MessageService) private readonly messageService: MessageService,
    ) { }

    registerCommands(registry: CommandRegistry): void {
        registry.registerCommand(HelloWorldCommand, {
            execute: () => this.messageService.info('Hello World!')
        });
    }
}

Can someone explain me injectable and inject (or above code snippet?)

From my vague understanding of DI, we pass dependency in class instead of importing and initialising it inside constructor.

In angular docs, it is mentioned

When using TypeScript, @Inject is only needed for injecting primitives.

I am not sure what primitives mean here

This similar question seems more like theoretical and hard to comprehend: what is diff in @Injectable() and @Inject

0

There are 0 answers