Skip to content

Commit 2187e67

Browse files
author
Arthur Ozga
committed
Get Widened Type
1 parent 533262c commit 2187e67

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ namespace ts {
8383
getSignaturesOfType,
8484
getIndexTypeOfType,
8585
getBaseTypes,
86+
getWidenedType,
8687
getTypeFromTypeNode,
8788
getParameterType: getTypeAtPosition,
8889
getReturnTypeOfSignature,

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,6 +3307,14 @@
33073307
"category": "Message",
33083308
"code": 90015
33093309
},
3310+
"Add declaration for missing property '{0}'": {
3311+
"category": "Message",
3312+
"code": 90016
3313+
},
3314+
"Add index accessor for missing property '{0}'": {
3315+
"category": "Message",
3316+
"code": 90017
3317+
},
33103318
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
33113319
"category": "Error",
33123320
"code": 8017

src/compiler/types.ts

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

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,61 @@ namespace ts.codefix {
1212
// This is the identifier in the case of a class declaration
1313
// or the class keyword token in the case of a class expression.
1414
const token = getTokenAtPosition(sourceFile, start);
15-
const checker = context.program.getTypeChecker();
1615

17-
if(!(token.parent && token.parent.kind === SyntaxKind.PropertyAccessExpression)) {
16+
const classDeclaration = getContainingClass(token);
17+
if (!classDeclaration) {
1818
return undefined;
1919
}
2020

21-
if((token.parent as PropertyAccessExpression).expression.kind !== SyntaxKind.ThisKeyword) {
21+
const startPos = classDeclaration.members.pos;
22+
23+
if (!(token.parent && token.parent.kind === SyntaxKind.PropertyAccessExpression)) {
2224
return undefined;
2325
}
24-
1 + 1;
26+
27+
if ((token.parent as PropertyAccessExpression).expression.kind !== SyntaxKind.ThisKeyword) {
28+
return undefined;
29+
}
30+
31+
// if function call, synthesize function declaration
32+
if(token.parent.parent.kind == SyntaxKind.CallExpression) {
33+
34+
}
2535

2636
let typeString: string = 'any';
37+
2738
// if binary expression, try to infer type for LHS, else use any
28-
if(token.parent.parent.kind === SyntaxKind.BinaryExpression)
29-
{
39+
if (token.parent.parent.kind === SyntaxKind.BinaryExpression) {
3040
const binaryExpression = token.parent.parent as BinaryExpression;
3141
binaryExpression.operatorToken;
32-
33-
const type = checker.getTypeAtLocation(binaryExpression.right);
42+
43+
const checker = context.program.getTypeChecker();
44+
const type = checker.getWidenedType(checker.getTypeAtLocation(binaryExpression.right));
3445
typeString = checker.typeToString(type);
3546
}
3647

37-
const classDeclaration = getContainingClass(token);
38-
const startPos = classDeclaration.members.pos;
3948
return [{
40-
description: getLocaleSpecificMessage(Diagnostics.Implement_inherited_abstract_class),
49+
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_declaration_for_missing_property_0), [token.getText()]),
4150
changes: [{
4251
fileName: sourceFile.fileName,
4352
textChanges: [{
4453
span: { start: startPos, length: 0 },
4554
newText: `${token.getFullText(sourceFile)}: ${typeString};`
4655
}]
4756
}]
57+
},
58+
{
59+
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_accessor_for_missing_property_0), [token.getText()]),
60+
changes: [{
61+
fileName: sourceFile.fileName,
62+
textChanges: [{
63+
span: { start: startPos, length: 0 },
64+
newText: `[name: string]: ${typeString};`
65+
}]
66+
}]
4867
}];
4968
}
5069

51-
52-
53-
54-
// x needs to be a `this` construct. ie
55-
// this.<thing>.
5670
// Want to infer type of x when possible. ie:
5771
// * assignment,
5872
// * function call argument: foo<T>(this.x) where foo(x: SomeType<T>)
@@ -65,10 +79,4 @@ namespace ts.codefix {
6579
// inferred type might be error. then add any.
6680
// either make indexable of the inferred type
6781
// add named member of the inferred type.
68-
}
69-
70-
// // class C {
71-
// // constructor() {
72-
// // this.x = 1;
73-
// // }
74-
// // }
82+
}

0 commit comments

Comments
 (0)