Skip to content

Commit 7b9ecbb

Browse files
authored
Fixed crash related to function expression type inference (#467)
* fixed crash when inferring function expression type in specific situation this occurred when a function expression was assigned to property in an object literal that itself was assigned to a type that didn't actually contain that property: let foo: {} = {bar: s => s}; * added test
1 parent dedee12 commit 7b9ecbb

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/TSHelper.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,10 @@ export class TSHelper {
484484
const objType = this.inferAssignedType(expression.parent.parent, checker);
485485
const property = objType.getProperty(expression.parent.name.getText());
486486
if (!property) {
487-
return objType.getStringIndexType();
487+
const stringPropertyType = objType.getStringIndexType();
488+
if (stringPropertyType) {
489+
return stringPropertyType;
490+
}
488491
} else {
489492
return checker.getTypeAtLocation(property.valueDeclaration);
490493
}

test/unit/assignments.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,14 @@ export class AssignmentTests {
875875
Expect(util.transpileAndExecute(code)).toBe("foobar");
876876
}
877877

878+
@Test("Function expression type inference in object literal assigned to narrower type")
879+
public functionEXpressionTypeInferenceInObjectLiteralAssignedToNarrowerType(): void {
880+
const code =
881+
`let foo: {} = {bar: s => s};
882+
return (foo as {bar: (a: any) => any}).bar("foobar");`;
883+
Expect(util.transpileAndExecute(code)).toBe("foobar");
884+
}
885+
878886
@TestCase("const foo: Foo", "s => s")
879887
@TestCase("const foo: Foo", "(s => s)")
880888
@TestCase("const foo: Foo", "function(s) { return s; }")

0 commit comments

Comments
 (0)