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

Generic callback functions and 'this' #1568

Closed
DanielAdolfsson opened this issue Aug 4, 2024 · 1 comment · Fixed by #1589
Closed

Generic callback functions and 'this' #1568

DanielAdolfsson opened this issue Aug 4, 2024 · 1 comment · Fixed by #1589
Labels
bug scope: transformation Transformation of TS to Lua

Comments

@DanielAdolfsson
Copy link

/** @noSelf */
declare namespace Test {
  export function testCallback<T extends (...args: any[]) => void>(
    callback: T,
  ): FunctionContainer<T>;
}

testCallback(() => {});

This fails with error TSTL: Unable to convert function with a 'this' parameter to function 'callback' with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'..

validateFunctionAssignment fails here. I could make it work by returning immediately if toType.isTypeParameter() is true, but I suspect that's not the correct solution to this particular problem.

@DanielAdolfsson
Copy link
Author

DanielAdolfsson commented Aug 4, 2024

That wasn't a very good solution, so I tried something else.

Since arrow functions are not supposed to have this anyway, I decided to add:

    if (ts.isArrowFunction(signatureDeclaration)) {
        return ContextType.Void;
    }

to computeDeclarationContextType. Maybe that is a more correct solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug scope: transformation Transformation of TS to Lua
Projects
None yet
2 participants