#Extending a dynamic class leads to error 2545
15 messages · Page 1 of 1 (latest)
Preview:ts ... // A mixin class must have a constructor with a single ...
export function mockService<T extends Class<Service, [string]>>(baseClass: T): Class<Service,[]> {
// A mixin class must have a constructor with a single rest parameter of type 'any[]'.(2545)
// But.. i AM doing it ??
return class extends baseClass {
constructor(...rest:any[]){
super("mocked name")
}
test(){
super.test()
console.log('Mocked method.')
}
}
}
I can get away by casting baseClass as any but i really want to make sure that the dependencies are given.
do you actually need mockService to be generic? T is only used in one place
this works:
Preview:ts ... export function mockService( baseClass: Class<Service, [string]> ): Class<Service, []> { ...
that error message is pretty bad for this situation though
Yeah i need it to be generic sadly 😦
I am using these classes as dependency injection tokens, I have teams actually extending a base class and using the derivated classes in their services
Oh well I can just safely cast it...
Preview:```ts
interface Class<T, TArgs extends unknown[]> {
new (...args: TArgs): T
}
interface Service {
test(): void
}
// This is a service i want to mock
class BaseService implements Service {
// i want to mock the dependencies
constructor(private na
...```
can you show me an example that uses the generic-ness?
It's something like that that we do
Preview:```ts
interface Class<T, TArgs extends unknown[]> {
new (...args: TArgs): T
}
interface Repository<T = any> {
get(id: string): void
remove(id: string): void
}
interface Database {}
function createStore<T>(): Class<
Repository<T>,
[Database]
{
...```
We use experimental metadata in our dependency injection system to automatically instanciate a DeleteCustomerHandler provided we know how to construct a Database
that still doesn't use the type parameter anywhere. it's equivalent to this: