Dependency Injection & using interfaces?

Letting your application components (the classes that contain the application logic) implement an interface is important, since this promotes the concept of: Program to an interface, not an implementation. This is effectively the Dependency Inversion Principle (DIP). Doing so allows you to replace, intercept or decorate dependencies without the need to change consumers of such … Read more

Why middleware in ASP.NET Core requires specific semantics, but not an interface?

The Invoke method is flexible and you can ask for additional parameters. ASP.NET will inject the additional parameters using the application’s service configuration. public async Task Invoke(HttpContext ctx, IHostingEnvironment host, ISomethingElse service) { // … } C# interface definitions can’t provide this flexibility in a nice way.

How is duck typing different from the old ‘variant’ type and/or interfaces?

In some of the answers here, I’ve seen some incorrect use of terminology, which has lead people to provide wrong answers. So, before I give my answer, I’m going to provide a few definitions: Strongly typed A language is strongly typed if it enforces the type safety of a program. That means that it guarantees … Read more

Implementing TypeScript interface with bare function signature plus other fields

A class cannot implement everything that is available in a typescript interface. Two prime examples are callable signatures and index operations e.g. : Implement an indexible interface The reason is that an interface is primarily designed to describe anything that JavaScript objects can do. Therefore it needs to be really robust. A TypeScript class however … Read more

What is an empty interface used for

Usually it’s to signal usage of a class. You can implement IMessage to signal that your class is a message. Other code can then use reflection to see if your objects are meant to be used as messages and act accordingly. This is something that was used in Java a lot before they had annotations. … Read more

Extend interface defined in .d.ts file

// How to extend Validator interface adding isArray() method?? You cannot do this in a file that is a module (some guidance here) and your file is a module because you have import expressValidator. Instead create a extendedValidator.d.ts and add the new stuff for TypeScript’s engine: declare module ExpressValidator { export interface Validator { isArray: … Read more