X Tutup
Skip to content

New functions - return values and context inference#304

Merged
Perryvw merged 30 commits intoTypeScriptToLua:new-functionsfrom
tomblind:new-functions
Dec 14, 2018
Merged

New functions - return values and context inference#304
Perryvw merged 30 commits intoTypeScriptToLua:new-functionsfrom
tomblind:new-functions

Conversation

@tomblind
Copy link
Copy Markdown
Collaborator

Return values are now run through validateAssignment so that methods can't be returned as functions and vice-versa.

declare function func(): void;
function returnsMethod(): (this: Foo) => void {
    return func; // Error
}

Function expressions passed as arguments can have their context type inferred now.

declare function takesMethod(method: (this: Foo, s: string) => string);
takesMethod(s => s); // Works - lambda will transpile with a dummy parameter

Same for return values:

function returnsMethod(): (this: Foo, s: string) => string {
    return s => s; // Works
}

Note that more complex cases will still fail to to properly infer the context type:

declare class Foo {}
interface Method {
    (this: Foo, s: string): string;
}
const a: Method[] = [s => s]; // Error - cannot convert function to method
const b: Method[] = [function(this: Foo, s) { return s; }]; // Workaround

src/TSHelper.ts Outdated
}
}

public static getFunctionReturnType(node: ts.Node, checker: ts.TypeChecker): ts.Type {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like getContainingFunctionReturnType would be clearer

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me.

@Perryvw Perryvw merged commit 01ae584 into TypeScriptToLua:new-functions Dec 14, 2018
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

Successfully merging this pull request may close these issues.

2 participants

X Tutup