X Tutup
Skip to content

Commit 4578c3c

Browse files
authored
Fix exception from function validation on functions with composite signatures (#1420)
1 parent a17d9b1 commit 4578c3c

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/transformation/utils/function-context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ function reduceContextTypes(contexts: ContextType[]): ContextType {
116116
}
117117

118118
function getSignatureDeclarations(context: TransformationContext, signature: ts.Signature): ts.SignatureDeclaration[] {
119+
if (signature.compositeSignatures) {
120+
return signature.compositeSignatures.flatMap(s => getSignatureDeclarations(context, s));
121+
}
122+
119123
const signatureDeclaration = signature.getDeclaration();
124+
if (signatureDeclaration === undefined) {
125+
return [];
126+
}
127+
120128
let inferredType: ts.Type | undefined;
121129
if (
122130
ts.isMethodDeclaration(signatureDeclaration) &&

src/typescript-internal.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ declare module "typescript" {
3535
parent?: Symbol;
3636
}
3737

38+
interface Signature {
39+
compositeSignatures?: Signature[];
40+
}
41+
3842
function transformJsx(context: TransformationContext): (x: SourceFile) => SourceFile;
3943

4044
export type OuterExpression =

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,18 @@ test.each([
226226
let f: ${assignType} = o;
227227
`.expectDiagnosticsToMatchSnapshot([unsupportedOverloadAssignment.code], true);
228228
});
229+
230+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/896
231+
test("Does not fail on union type signatures (#896)", () => {
232+
util.testExpression`foo<'a'>(() => {});`
233+
.setTsHeader(
234+
`
235+
declare interface Events {
236+
a(): void;
237+
[key: string]: (this: void) => void;
238+
}
239+
declare function foo<T extends 'a' | 'b'>(callback: Events[T]): void;
240+
`
241+
)
242+
.expectToHaveDiagnostics([unsupportedOverloadAssignment.code]);
243+
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,18 @@ test.each([
233233
return f(${args.map(a => '"' + a + '"').join(", ")});
234234
`.expectToMatchJsResult();
235235
});
236+
237+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/896
238+
test("Does not fail on union type signatures (#896)", () => {
239+
util.testExpression`foo<'a'>(() => {});`
240+
.setTsHeader(
241+
`
242+
declare interface Events {
243+
a(): void;
244+
[key: string]: Function;
245+
}
246+
declare function foo<T extends 'a' | 'b'>(callback: Events[T]): void;
247+
`
248+
)
249+
.expectToHaveNoDiagnostics();
250+
});

0 commit comments

Comments
 (0)
X Tutup