Skip to content

Commit 1655679

Browse files
committed
Resolve bug inferring context type from generic functions in type parameters
1 parent da0774c commit 1655679

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/transformation/utils/function-context.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as ts from "typescript";
22
import { CompilerOptions } from "../../CompilerOptions";
33
import { TransformationContext } from "../context";
44
import { AnnotationKind, getFileAnnotations, getNodeAnnotations } from "./annotations";
5-
import { findFirstNodeAbove, getAllCallSignatures, inferAssignedType } from "./typescript";
5+
import { findFirstNodeAbove, findFirstNonOuterParent, getAllCallSignatures, inferAssignedType } from "./typescript";
66

77
export enum ContextType {
88
None = 0,
@@ -118,6 +118,13 @@ function computeDeclarationContextType(context: TransformationContext, signature
118118
return ContextType.Void;
119119
}
120120

121+
if (
122+
ts.isArrowFunction(signatureDeclaration) &&
123+
ts.isCallExpression(findFirstNonOuterParent(signatureDeclaration))
124+
) {
125+
return ContextType.Void;
126+
}
127+
121128
if (
122129
ts.isMethodSignature(signatureDeclaration) ||
123130
ts.isMethodDeclaration(signatureDeclaration) ||

test/unit/functions/validation/validFunctionAssignments.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,21 @@ test("Does not fail on union type signatures (#896)", () => {
248248
)
249249
.expectToHaveNoDiagnostics();
250250
});
251+
252+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1568
253+
test("No false positives when using generic functions (#1568)", () => {
254+
util.testModule`
255+
/** @noSelf */
256+
declare namespace Test {
257+
export function testCallback<T extends (...args: any[]) => void>(
258+
callback: T,
259+
): void;
260+
export function testCallback2(
261+
callback: (...args: any[]) => void,
262+
): void;
263+
}
264+
265+
Test.testCallback(() => {});
266+
Test.testCallback2(() => {});
267+
`.expectToHaveNoDiagnostics();
268+
});

0 commit comments

Comments
 (0)