Skip to content

Commit f110480

Browse files
authored
Merge pull request #33263 from microsoft/infer-from-usage/similarity-to-builtins
Infer from usage by similarity to builtin types
2 parents bf992a5 + 84e857b commit f110480

17 files changed

+314
-99
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ namespace ts {
523523
},
524524
getApparentType,
525525
getUnionType,
526+
isTypeAssignableTo: (source, target) => {
527+
return isTypeAssignableTo(source, target);
528+
},
526529
createAnonymousType,
527530
createSignature,
528531
createSymbol,

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,6 +3315,7 @@ namespace ts {
33153315
/* @internal */ getElementTypeOfArrayType(arrayType: Type): Type | undefined;
33163316
/* @internal */ createPromiseType(type: Type): Type;
33173317

3318+
/* @internal */ isTypeAssignableTo(source: Type, target: Type): boolean;
33183319
/* @internal */ createAnonymousType(symbol: Symbol, members: SymbolTable, callSignatures: Signature[], constructSignatures: Signature[], stringIndexInfo: IndexInfo | undefined, numberIndexInfo: IndexInfo | undefined): Type;
33193320
/* @internal */ createSignature(
33203321
declaration: SignatureDeclaration,

src/services/codefixes/inferFromUsage.ts

Lines changed: 222 additions & 92 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function inferAny( [| app |] ) {
5+
//// const result = app.use('hi')
6+
//// return result
7+
//// }
8+
9+
verify.rangeAfterCodeFix("app: { use: (arg0: string) => any; }");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function inferVoid( [| app |] ) {
5+
//// app.use('hi')
6+
//// }
7+
8+
verify.rangeAfterCodeFix("app: { use: (arg0: string) => void; }");

tests/cases/fourslash/codeFixInferFromFunctionUsage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
// @noImplicitAny: true
44
////function wrap( [| arr |] ) {
5-
//// arr.sort(function (a: number, b: number) { return a < b ? -1 : 1 })
5+
//// arr.other(function (a: number, b: number) { return a < b ? -1 : 1 })
66
//// }
77

88
// https://github.com/Microsoft/TypeScript/issues/29330
9-
verify.rangeAfterCodeFix("arr: { sort: (arg0: (a: number, b: number) => 1 | -1) => void; }");
9+
verify.rangeAfterCodeFix("arr: { other: (arg0: (a: number, b: number) => 1 | -1) => void; }");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function wrap( [| s |] ) {
5+
//// return s.length + s.indexOf('hi')
6+
//// }
7+
8+
// https://github.com/Microsoft/TypeScript/issues/29330
9+
verify.rangeAfterCodeFix("s: string | string[]");
10+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function foo([|a, m |]) {
5+
//// return a + m
6+
//// }
7+
8+
verify.rangeAfterCodeFix("a: any, m: any", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
9+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function foo([|p, a, b, c, d, e |]) {
5+
//// var x: string = a.pop()
6+
//// b.reverse()
7+
//// var rr: boolean[] = c.reverse()
8+
//// d.some(t => t > 1); // can't infer from callbacks right now
9+
//// var y = e.concat(12); // can't infer from overloaded functions right now
10+
//// return p.push(12)
11+
//// }
12+
13+
verify.rangeAfterCodeFix("p: number[], a: string[], b: any[], c: boolean[], d: any[], e: any[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
14+

tests/cases/fourslash/codeFixInferFromUsageCallBodyBoth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
////class C {
4-
////
4+
//// p = 2
55
////}
66
////var c = new C()
77
////function f([|x, y |]) {

0 commit comments

Comments
 (0)