Skip to content

Commit f2770a1

Browse files
author
Arthur Ozga
committed
widen type, index signature, and add tests
1 parent a4cf12e commit f2770a1

10 files changed

+108
-7
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ namespace ts {
8484
getIndexTypeOfType,
8585
getBaseTypes,
8686
getBaseTypeOfLiteralType,
87+
getWidenedType,
8788
getTypeFromTypeNode,
8889
getParameterType: getTypeAtPosition,
8990
getReturnTypeOfSignature,

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3311,7 +3311,7 @@
33113311
"category": "Message",
33123312
"code": 90016
33133313
},
3314-
"Add index accessor for missing property '{0}'": {
3314+
"Add index signature for missing property '{0}'": {
33153315
"category": "Message",
33163316
"code": 90017
33173317
},

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,6 +2383,7 @@
23832383
getIndexTypeOfType(type: Type, kind: IndexKind): Type;
23842384
getBaseTypes(type: InterfaceType): BaseType[];
23852385
getBaseTypeOfLiteralType(type: Type): Type;
2386+
getWidenedType(type: Type): Type;
23862387
getReturnTypeOfSignature(signature: Signature): Type;
23872388
/**
23882389
* Gets the type of a parameter at a given position in a signature.

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace ts.codefix {
3737
const binaryExpression = token.parent.parent as BinaryExpression;
3838

3939
const checker = context.program.getTypeChecker();
40-
const widenedType = checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right));
40+
const widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(binaryExpression.right)));
4141
typeString = checker.typeToString(widenedType);
4242
}
4343

@@ -54,7 +54,7 @@ namespace ts.codefix {
5454
}]
5555
},
5656
{
57-
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_accessor_for_missing_property_0), [token.getText()]),
57+
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_signature_for_missing_property_0), [token.getText()]),
5858
changes: [{
5959
fileName: sourceFile.fileName,
6060
textChanges: [{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// [|class A {
4+
//// constructor() {
5+
//// this.x = 10;
6+
//// }
7+
//// }|]
8+
9+
verify.rangeAfterCodeFix(`
10+
class A {
11+
[name: string]: number;
12+
13+
constructor() {
14+
this.x = 10;
15+
}
16+
}
17+
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 1);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// [|class A {
4+
//// constructor() {
5+
//// this.x = function(x: number, y?: A){
6+
//// return x > 0 ? x : y;
7+
//// }
8+
//// }
9+
//// }|]
10+
11+
verify.rangeAfterCodeFix(`
12+
class A {
13+
x: (x: number, y?: A) => A;
14+
constructor() {
15+
this.x = function(x: number, y?: A){
16+
return x > 0 ? x : y;
17+
}
18+
}
19+
}
20+
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// [|class A {
4+
//// y: number;
5+
//// constructor(public a: number) {
6+
//// this.x = function(x: number, y?: A){
7+
//// return x > 0 ? x : y;
8+
//// }
9+
//// }
10+
//// }|]
11+
12+
verify.rangeAfterCodeFix(`
13+
class A {
14+
x: (x: number, y?: A) => number | A;
15+
y: number;
16+
constructor(public a: number) {
17+
this.x = function(x: number, y?: A){
18+
return x > 0 ? x : y;
19+
}
20+
}
21+
}
22+
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);

tests/cases/fourslash/codeFixUndeclaredPropertyObjectLiteral.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
//// [|class A {
44
//// constructor() {
5-
//// this.x = { a: 10, b: "hello" };
5+
//// let e: any = 10;
6+
//// this.x = { a: 10, b: "hello", c: undefined, d: null, e: e };
67
//// }
78
//// }|]
89

910
verify.rangeAfterCodeFix(`
1011
class A {
11-
x: { a: number; b: string; };
12+
x: { a: number; b: string; c: any; d: any; e: any; };
1213
1314
constructor() {
14-
this.x = { a: 10, b: "hello" };
15+
let e: any = 10;
16+
this.x = { a: 10, b: "hello", c: undefined, d: null, e: e };
1517
}
1618
}
17-
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
19+
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strictNullChecks: true
4+
5+
//// [|class A {
6+
//// constructor() {
7+
//// let e: any = 10;
8+
//// this.x = { a: 10, b: "hello", c: undefined, d: null, e: e };
9+
//// }
10+
//// }|]
11+
12+
verify.rangeAfterCodeFix(`
13+
class A {
14+
x: { a: number; b: string; c: undefined; d: null; e: any; };
15+
16+
constructor() {
17+
let e: any = 10;
18+
this.x = { a: 10, b: "hello", c: undefined, d: null, e: e };
19+
}
20+
}
21+
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// [|class A {
4+
//// constructor() {
5+
//// this.mythis = this;
6+
//// }
7+
//// }|]
8+
9+
verify.rangeAfterCodeFix(`
10+
class A {
11+
mythis: this;
12+
13+
constructor() {
14+
this.mythis = this;
15+
}
16+
}
17+
`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);

0 commit comments

Comments
 (0)