Angular · Senior · Question 21 of 75

Why use an abstract class as a DI token?

export abstract class Storage { abstract get(key: string): string | null; }
providers: [{ provide: Storage, useClass: LocalStorageService }]

Explanation

Interfaces are erased at compile time and can't be tokens. An abstract class gives both a type and a runtime token, which is the Angular way to depend on abstractions.

Further reading

Problem with a question

Angular · Senior · question 21

Test yourself

Take the Angular · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz