'Generic variant of the TestBed module provider declaration

Since for some test scenarios I need to provide lots of services, I wanted to speed up things by using a convenient helper method instead of writing it in relatively verbose way you have to define your providers in the TestBed module.

So first step was this method, which works fine:

export function provideSpecific<T>(type, value) {
  return {provide: type, useValue: value};
}

Now I wanted to make things as compact as possible but then it stops working:

export function provide<T>(value: T) {
  return {provide: typeof(value), useValue: value as T}
};

I am relatively new to JS/TS so I don't really know what I should look for. With the debugger I found out, that the provide argument is a function. Usually you just pass the type directly (i.e. { provide: MatDialog, ... }). So I assumed either typeof(value) or Object.getPrototypeOf(value).constructor would do the trick. But neither did.

Is this somehow solvable? What is expected as those two values? Am I missing something there?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source