Skip to content

Commit 9de8dbb

Browse files
authored
fix(42833): add return type to methods with overloads (#42881)
1 parent b0474dd commit 9de8dbb

7 files changed

+18
-9
lines changed

src/services/codefixes/helpers.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ namespace ts.codefix {
138138
}
139139
else {
140140
Debug.assert(declarations.length === signatures.length, "Declarations and signatures should match count");
141-
addClassElement(createMethodImplementingSignatures(signatures, name, optional, modifiers, quotePreference));
141+
addClassElement(createMethodImplementingSignatures(checker, context, enclosingDeclaration, signatures, name, optional, modifiers, quotePreference));
142142
}
143143
}
144144
break;
@@ -338,6 +338,9 @@ namespace ts.codefix {
338338
}
339339

340340
function createMethodImplementingSignatures(
341+
checker: TypeChecker,
342+
context: TypeConstructionContext,
343+
enclosingDeclaration: ClassLikeDeclaration,
341344
signatures: readonly Signature[],
342345
name: PropertyName,
343346
optional: boolean,
@@ -362,7 +365,6 @@ namespace ts.codefix {
362365
}
363366
const maxNonRestArgs = maxArgsSignature.parameters.length - (signatureHasRestParameter(maxArgsSignature) ? 1 : 0);
364367
const maxArgsParameterSymbolNames = maxArgsSignature.parameters.map(symbol => symbol.name);
365-
366368
const parameters = createDummyParameters(maxNonRestArgs, maxArgsParameterSymbolNames, /* types */ undefined, minArgumentCount, /*inJs*/ false);
367369

368370
if (someSigHasRestParameter) {
@@ -384,10 +386,17 @@ namespace ts.codefix {
384386
optional,
385387
/*typeParameters*/ undefined,
386388
parameters,
387-
/*returnType*/ undefined,
389+
getReturnTypeFromSignatures(signatures, checker, context, enclosingDeclaration),
388390
quotePreference);
389391
}
390392

393+
function getReturnTypeFromSignatures(signatures: readonly Signature[], checker: TypeChecker, context: TypeConstructionContext, enclosingDeclaration: ClassLikeDeclaration): TypeNode | undefined {
394+
if (length(signatures)) {
395+
const type = checker.getUnionType(map(signatures, checker.getReturnTypeOfSignature));
396+
return checker.typeToTypeNode(type, enclosingDeclaration, /*flags*/ undefined, getNoopSymbolTrackerWithResolver(context));
397+
}
398+
}
399+
391400
function createStubbedMethod(
392401
modifiers: readonly Modifier[] | undefined,
393402
name: PropertyName,

tests/cases/fourslash/codeFixClassExtendAbstractMethod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class C extends A {
2626
f(a: number, b: string): this;
2727
f(a: string, b: number): Function;
2828
f(a: string): Function;
29-
f(a: any, b?: any) {
29+
f(a: any, b?: any): boolean | Function | this {
3030
throw new Error("Method not implemented.");
3131
}
3232
foo(): number {

tests/cases/fourslash/codeFixClassImplementInterfaceComputedPropertyNameWellKnownSymbols.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class C implements I<number> {
6262
[Symbol.toPrimitive](hint: "number"): number;
6363
[Symbol.toPrimitive](hint: "default"): number;
6464
[Symbol.toPrimitive](hint: "string"): string;
65-
[Symbol.toPrimitive](hint: any) {
65+
[Symbol.toPrimitive](hint: any): string | number {
6666
throw new Error("Method not implemented.");
6767
}
6868
[Symbol.toStringTag]: string;

tests/cases/fourslash/codeFixClassImplementInterfaceMethodTypePredicate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ verify.codeFix({
1818
class C implements I {
1919
f(i: any): i is I;
2020
f(): this is I;
21-
f(i?: any) {
21+
f(i?: any): boolean {
2222
throw new Error("Method not implemented.");
2323
}
2424
}`,

tests/cases/fourslash/codeFixClassImplementInterfaceMultipleSignatures.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class C implements I {
2121
method(a: number, b: string): boolean;
2222
method(a: string, b: number): Function;
2323
method(a: string): Function;
24-
method(a: any, b?: any) {
24+
method(a: any, b?: any): boolean | Function {
2525
throw new Error("Method not implemented.");
2626
}
2727
}`,

tests/cases/fourslash/codeFixClassImplementInterfaceMultipleSignaturesRest1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class C implements I {
2121
method(a: number, ...b: string[]): boolean;
2222
method(a: string, ...b: number[]): Function;
2323
method(a: string): Function;
24-
method(a: any, ...b?: any[]) {
24+
method(a: any, ...b?: any[]): boolean | Function {
2525
throw new Error("Method not implemented.");
2626
}
2727
}`,

tests/cases/fourslash/codeFixClassImplementInterfaceMultipleSignaturesRest2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class C implements I {
2121
method(a: number, ...b: string[]): boolean;
2222
method(a: string, b: number): Function;
2323
method(a: string): Function;
24-
method(a: any, b?: any, ...rest?: any[]) {
24+
method(a: any, b?: any, ...rest?: any[]): boolean | Function {
2525
throw new Error("Method not implemented.");
2626
}
2727
}`,

0 commit comments

Comments
 (0)