mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
fix(42833): add return type to methods with overloads (#42881)
This commit is contained in:
parent
b0474dd482
commit
9de8dbbfb8
@ -138,7 +138,7 @@ namespace ts.codefix {
|
||||
}
|
||||
else {
|
||||
Debug.assert(declarations.length === signatures.length, "Declarations and signatures should match count");
|
||||
addClassElement(createMethodImplementingSignatures(signatures, name, optional, modifiers, quotePreference));
|
||||
addClassElement(createMethodImplementingSignatures(checker, context, enclosingDeclaration, signatures, name, optional, modifiers, quotePreference));
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -338,6 +338,9 @@ namespace ts.codefix {
|
||||
}
|
||||
|
||||
function createMethodImplementingSignatures(
|
||||
checker: TypeChecker,
|
||||
context: TypeConstructionContext,
|
||||
enclosingDeclaration: ClassLikeDeclaration,
|
||||
signatures: readonly Signature[],
|
||||
name: PropertyName,
|
||||
optional: boolean,
|
||||
@ -362,7 +365,6 @@ namespace ts.codefix {
|
||||
}
|
||||
const maxNonRestArgs = maxArgsSignature.parameters.length - (signatureHasRestParameter(maxArgsSignature) ? 1 : 0);
|
||||
const maxArgsParameterSymbolNames = maxArgsSignature.parameters.map(symbol => symbol.name);
|
||||
|
||||
const parameters = createDummyParameters(maxNonRestArgs, maxArgsParameterSymbolNames, /* types */ undefined, minArgumentCount, /*inJs*/ false);
|
||||
|
||||
if (someSigHasRestParameter) {
|
||||
@ -384,10 +386,17 @@ namespace ts.codefix {
|
||||
optional,
|
||||
/*typeParameters*/ undefined,
|
||||
parameters,
|
||||
/*returnType*/ undefined,
|
||||
getReturnTypeFromSignatures(signatures, checker, context, enclosingDeclaration),
|
||||
quotePreference);
|
||||
}
|
||||
|
||||
function getReturnTypeFromSignatures(signatures: readonly Signature[], checker: TypeChecker, context: TypeConstructionContext, enclosingDeclaration: ClassLikeDeclaration): TypeNode | undefined {
|
||||
if (length(signatures)) {
|
||||
const type = checker.getUnionType(map(signatures, checker.getReturnTypeOfSignature));
|
||||
return checker.typeToTypeNode(type, enclosingDeclaration, /*flags*/ undefined, getNoopSymbolTrackerWithResolver(context));
|
||||
}
|
||||
}
|
||||
|
||||
function createStubbedMethod(
|
||||
modifiers: readonly Modifier[] | undefined,
|
||||
name: PropertyName,
|
||||
|
||||
@ -26,7 +26,7 @@ class C extends A {
|
||||
f(a: number, b: string): this;
|
||||
f(a: string, b: number): Function;
|
||||
f(a: string): Function;
|
||||
f(a: any, b?: any) {
|
||||
f(a: any, b?: any): boolean | Function | this {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
foo(): number {
|
||||
|
||||
@ -62,7 +62,7 @@ class C implements I<number> {
|
||||
[Symbol.toPrimitive](hint: "number"): number;
|
||||
[Symbol.toPrimitive](hint: "default"): number;
|
||||
[Symbol.toPrimitive](hint: "string"): string;
|
||||
[Symbol.toPrimitive](hint: any) {
|
||||
[Symbol.toPrimitive](hint: any): string | number {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
[Symbol.toStringTag]: string;
|
||||
|
||||
@ -18,7 +18,7 @@ verify.codeFix({
|
||||
class C implements I {
|
||||
f(i: any): i is I;
|
||||
f(): this is I;
|
||||
f(i?: any) {
|
||||
f(i?: any): boolean {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}`,
|
||||
|
||||
@ -21,7 +21,7 @@ class C implements I {
|
||||
method(a: number, b: string): boolean;
|
||||
method(a: string, b: number): Function;
|
||||
method(a: string): Function;
|
||||
method(a: any, b?: any) {
|
||||
method(a: any, b?: any): boolean | Function {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}`,
|
||||
|
||||
@ -21,7 +21,7 @@ class C implements I {
|
||||
method(a: number, ...b: string[]): boolean;
|
||||
method(a: string, ...b: number[]): Function;
|
||||
method(a: string): Function;
|
||||
method(a: any, ...b?: any[]) {
|
||||
method(a: any, ...b?: any[]): boolean | Function {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}`,
|
||||
|
||||
@ -21,7 +21,7 @@ class C implements I {
|
||||
method(a: number, ...b: string[]): boolean;
|
||||
method(a: string, b: number): Function;
|
||||
method(a: string): Function;
|
||||
method(a: any, b?: any, ...rest?: any[]) {
|
||||
method(a: any, b?: any, ...rest?: any[]): boolean | Function {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}`,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user