fix(41827): allow infer parameters from method signature usage (#41836)

This commit is contained in:
Oleksandr T 2020-12-08 00:00:46 +02:00 committed by GitHub
parent 71e881a7ee
commit 0fa41db6c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -453,6 +453,7 @@ namespace ts.codefix {
break;
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
searchToken = containingFunction.name;
break;
}

View File

@ -0,0 +1,52 @@
/// <reference path='fourslash.ts' />
////interface Options {
//// run(a, b, c, d, e, f): void;
////}
////
////interface I {
//// x: number;
////}
////
////enum E {
//// X
////}
////
////function foo(options: Options) {
//// const a = 1;
//// const b = "";
//// const c = { x: 1, y: 1 };
//// const d = [1, 2, 3];
//// const e: I = { x: 1 };
//// const f: E = E.X;
////
//// options.run(a, b, c, d, e, f);
////}
verify.codeFixAll({
fixId: "inferFromUsage",
fixAllDescription: "Infer all types from usage",
newFileContent:
`interface Options {
run(a: number, b: string, c: { x: number; y: number; }, d: number[], e: I, f: E): void;
}
interface I {
x: number;
}
enum E {
X
}
function foo(options: Options) {
const a = 1;
const b = "";
const c = { x: 1, y: 1 };
const d = [1, 2, 3];
const e: I = { x: 1 };
const f: E = E.X;
options.run(a, b, c, d, e, f);
}`,
});