dvabuzyarov / moq.ts

Moq for Typescript
Apache License 2.0
120 stars 9 forks source link

Type-safe callback args #1058

Open dstoyanoff opened 7 months ago

dstoyanoff commented 7 months ago

Feature request

Make callback accept a generic argument to type the the args returned in the Expression

dvabuzyarov commented 7 months ago

Hello, @dstoyanoff
could you provide an example of the issue? It would help a lot to stay on the same page

dstoyanoff commented 7 months ago

Hey, sure.

Imagine I have a database repository and I want to mock a getById(id: string): User function for multiple users. I could of course do that for every user, but since callback give us access to args, we could do it dynamically:

repoMock
   .setup(repo => repo.getById('user_1'))
   .callback(({ args }) => ({
     id: args[0],
     // ...some other props
   }))

Since args are of type any[], there is no type-safety in reading them, so it would be nice if we could do that the same way as it is for It.Is<T>