Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/transformation/utils/function-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,25 @@ function getSignatureDeclarations(
): ts.SignatureDeclaration[] {
return signatures.flatMap(signature => {
const signatureDeclaration = signature.getDeclaration();
let inferredType: ts.Type | undefined;
if (
ts.isMethodDeclaration(signatureDeclaration) &&
ts.isObjectLiteralExpression(signatureDeclaration.parent) &&
!getExplicitThisParameter(signatureDeclaration)
) {
inferredType = context.checker.getContextualTypeForObjectLiteralElement(signatureDeclaration);
} else if (
(ts.isFunctionExpression(signatureDeclaration) || ts.isArrowFunction(signatureDeclaration)) &&
!getExplicitThisParameter(signatureDeclaration)
) {
// Infer type of function expressions/arrow functions
const inferredType = inferAssignedType(context, signatureDeclaration);
if (inferredType) {
const inferredSignatures = getAllCallSignatures(inferredType);
if (inferredSignatures.length > 0) {
return inferredSignatures.map(s => s.getDeclaration());
}
inferredType = inferAssignedType(context, signatureDeclaration);
}

if (inferredType) {
const inferredSignatures = getAllCallSignatures(inferredType);
if (inferredSignatures.length > 0) {
return inferredSignatures.map(s => s.getDeclaration());
}
}

Expand Down
1 change: 1 addition & 0 deletions src/typescript-internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ declare module "typescript" {

interface TypeChecker {
getElementTypeOfArrayType(type: Type): Type | undefined;
getContextualTypeForObjectLiteralElement(element: ObjectLiteralElementLike): Type | undefined;
}
}
63 changes: 63 additions & 0 deletions test/unit/objectLiteral.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,66 @@ test.each(['{x: "foobar"}.x', '{x: "foobar"}["x"]', '{x: () => "foobar"}.x()', '
util.testExpression(expression).expectToMatchJsResult();
}
);

describe("noSelf in functions", () => {
test("Explicit this: void parameter", () => {
// language=TypeScript
util.testFunction`
const obj: Record<string, (this: void, arg: string) => string> = {
method(a) {
return a;
},
func: function(a) {
return a;
},
arrow: (a) => {
return a;
}
};
return [obj.method("a") ?? "nil", obj.func("b") ?? "nil", obj.arrow("c") ?? "nil"];
`.expectToMatchJsResult();
});

test("No self annotation", () => {
// language=TypeScript
util.testFunction`
const obj: Record<string, /** @noSelf */(arg: string) => string> = {
method(a) {
return a;
},
func: function(a) {
return a;
},
arrow: (a) => {
return a;
}
};
return [obj.method("a") ?? "nil", obj.func("b") ?? "nil", obj.arrow("c") ?? "nil"];
`.expectToMatchJsResult();
});

test("individual function types", () => {
// language=TypeScript
util.testFunction`
interface FunctionContainer {
method: (this: void, a: string) => string
func: (this: void, a: string) => string
arrow: (this: void, a: string) => string
}

const obj: FunctionContainer = {
method(a) {
return a
},
func: function(a) {
return a
},
arrow: (a) => {
return a
}
}

return [obj.method("a") ?? "nil", obj.func("b") ?? "nil", obj.arrow("c") ?? "nil"];
`.expectToMatchJsResult();
});
});