Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support interfaces with call signatures #81

Closed
joshuavial opened this issue Oct 29, 2019 · 4 comments
Closed

support interfaces with call signatures #81

joshuavial opened this issue Oct 29, 2019 · 4 comments

Comments

@joshuavial
Copy link

Great library - I've been trying to extend it to support call signatures in an interface but am getting a little lost.

This is the code that I'd like to work

    interface InterfaceDefiningMethod {
        (): string;
    }

    interface Interface {
        a: InterfaceDefiningMethod;
    }

    it('should set the functions', () => {
        const properties: Interface = createMock<Interface>();
        expect(properties.a()).toBe('');
    });

I've played around with using typeChecker.getSignaturesOfType to get hold of the call signature but am struggling to find the right way to return a function.

joshuavial@21dbe1d has an example of what I've been trying - any tips on the best way to solve this?

@joshuavial joshuavial changed the title support interfaces with callsignatures support interfaces with call signatures Oct 29, 2019
@uittorio
Copy link
Member

uittorio commented Oct 29, 2019

Hi Joshua, thank you for spending time using the library and improving it. You are right, the library doesnt currently support interface call signature, however extending it to fully support it will require a bit more work because an interface can also have properties.

Let's say we have an interface

interface InterfaceWithCallSignature {
            (a: number): number;
            (a: string): string;
            b: string;
}

There are few requirements.

  1. should be callable (like you mentioned)
  2. should be able to choose wich overload to use. We could support both but it wil require even more work where we will create a function that based on the arguments type will return different types
  3. should have properties.

It's all possible but I would concentrate on point 1 and 3.
Here is an example of how to support in javascript that interface

const interfaceWithCallSignature: InterfaceWithCallSignature = Object.assign(function(a: number | string): any {
                if (typeof a === 'string') {
                    return a;
                } else {
                    return a;
                }
            }, {
                b: '',
            });

To reach this point we have to change the way the library generate the output.
Right now its creating an object with getter and setter for each properties. To support this we would have to filter out the callable properties and do an object assign with the combination of possible value to return.

I will work on a POC this evening.

If you want to investigate where the exported object is generated in ts-auto-mock you can look into mockCall.ts link thats where its receiveing the properties.

@uittorio
Copy link
Member

@joshuavial I've created a PR that will solve this issue.
#82.

@uittorio
Copy link
Member

I've just published a new version -> 1.2.1 that will cover this scenario. I'll leave the issue open until you've tested it :)

@joshuavial
Copy link
Author

Yep, works perfectly. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants