#Extending a dynamic class leads to error 2545

15 messages · Page 1 of 1 (latest)

floral stone
#

Hello ! I am trying to do a mocking system for a class. In my real life example, the dependencies of the class to mock are complex, i want strong typings

sick ploverBOT
#
Quadristan#2590

Preview:ts ... // A mixin class must have a constructor with a single ...

floral stone
#
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.

young juniper
#

do you actually need mockService to be generic? T is only used in one place

#

this works:

sick ploverBOT
#
mkantor#7432

Preview:ts ... export function mockService( baseClass: Class<Service, [string]> ): Class<Service, []> { ...

young juniper
#

that error message is pretty bad for this situation though

floral stone
#

Oh well I can just safely cast it...

sick ploverBOT
#
Quadristan#2590

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
...```

young juniper
#

can you show me an example that uses the generic-ness?

floral stone
#

It's something like that that we do

sick ploverBOT
#
Quadristan#2590

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]

{
...```

floral stone
#

We use experimental metadata in our dependency injection system to automatically instanciate a DeleteCustomerHandler provided we know how to construct a Database

young juniper
#

that still doesn't use the type parameter anywhere. it's equivalent to this: