What's the difference whether you "implements OnInit" when you use the function ngOnInit

I am asuming you are using Typescript?

You don't have to implement OnInit as this is Typescript and JavaScript does not have a concept of Interfaces. So when you implement and interface, this does not actually exist at run time.

Implementing an interface gives you all the strong type checking, and the TSC will throw out any warnings.

So i'd recommend you still to use the interface for this reason, plus it's good practice.


There is no difference. It's only good style to add implements OnInit.

ngOnInit is the only method this interface requires.

https://angular.io/api/core/OnInit


There is literally no difference between the two if you are talking about the "compiled" version (after TypeScript processed to runnable JavaScript). The Interface will just be removed (there are no TypeScript Interfaces at runtime). Except, the only real difference is that some Angular Tooling could make use of that information (static analysis, etc). However if you do not use any Tooling this information is just for yourself, respectively for all coders of the project. You could easily remove that rule from your tslint config and everything would keep working the same way.

Tags:

Angular