Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/services/codefixes/inferFromUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ namespace ts.codefix {

switch (node.parent.kind) {
case SyntaxKind.ExpressionStatement:
addCandidateType(usage, checker.getVoidType());
inferTypeFromExpressionStatement(node, usage);
break;
case SyntaxKind.PostfixUnaryExpression:
usage.isNumber = true;
Expand Down Expand Up @@ -668,6 +668,10 @@ namespace ts.codefix {
}
}

function inferTypeFromExpressionStatement(node: Expression, usage: Usage): void {
addCandidateType(usage, isCallExpression(node) ? checker.getVoidType() : checker.getAnyType());
}

function inferTypeFromPrefixUnaryExpression(node: PrefixUnaryExpression, usage: Usage): void {
switch (node.operator) {
case SyntaxKind.PlusPlusToken:
Expand Down Expand Up @@ -967,10 +971,7 @@ namespace ts.codefix {
if (usage.numberIndex) {
types.push(checker.createArrayType(combineFromUsage(usage.numberIndex)));
}
if (usage.properties && usage.properties.size
|| usage.calls && usage.calls.length
|| usage.constructs && usage.constructs.length
|| usage.stringIndex) {
if (usage.properties?.size || usage.calls?.length || usage.constructs?.length || usage.stringIndex) {
types.push(inferStructuralType(usage));
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ namespace ts.textChanges {
}
}
else {
endNode = node.kind !== SyntaxKind.VariableDeclaration && node.questionToken ? node.questionToken : node.name;
endNode = (node.kind === SyntaxKind.VariableDeclaration ? node.exclamationToken : node.questionToken) ?? node.name;
}

this.insertNodeAt(sourceFile, endNode.end, type, { prefix: ": " });
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/fourslash/codeFixInferFromUsageParameterLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />

// @noImplicitAny: true
//// function foo([|text |]) {
//// text.length;
//// text.indexOf("z");
//// text.charAt(0);
//// }

verify.rangeAfterCodeFix("text: string", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
//// return x.y.z
////}

verify.rangeAfterCodeFix("a: { b: { c: void; }; }, m: { n: () => number; }, x: { y: { z: number[]; }; }", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
verify.rangeAfterCodeFix("a: { b: { c: any; }; }, m: { n: () => number; }, x: { y: { z: number[]; }; }", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an unintended positive bug fix, right? Other places (e.g. codeFixInferFromCallInAssignment) infer any for variable types that would otherwise be describable as unknown.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by "unintended positive". void was wrong and any is right as far as I can tell.

I think, in most positions, inferring any vs unknown is a matter of preference -- how strict do you want the result to be? Right now I assume that people want pretty loose results, since I expect people will use this after converting from .js to .ts, and before trying to get rid of any explicit any.

We could discuss adding another fix to the codefix, one that defaults to unknown when possible. I think it would be a decent idea, but hard to explain in the space of a codefix title.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes I think we're on the same page - I meant that the change from void to any is positive, and I didn't initially intend it as part of this PR's changes. 👍

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ verify.codeFix({
index: 0,
newFileContent:
`/**
* @param {{ b: { c: void; }; }} a
* @param {{ b: { c: any; }; }} a
* @param {{ n: () => number; }} m
* @param {{ y: { z: number[]; }; }} x
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/fourslash/codeFixInferFromUsageVariableLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />

// @noImplicitAny: true
//// let [|text! |];
//// text.length;
//// text.indexOf("z");
//// text.charAt(0);

verify.rangeAfterCodeFix("text!: string", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);