Skip to content

Commit 9efab94

Browse files
committed
Annotate fewer extracted constants with types
Expose `isContextSensitive` from the checker and omit type annotations for expressions for which it returns false.
1 parent e9ac87c commit 9efab94

4 files changed

Lines changed: 7 additions & 4 deletions

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ namespace ts {
171171
node = getParseTreeNode(node, isExpression);
172172
return node ? getContextualType(node) : undefined;
173173
},
174+
isContextSensitive,
174175
getFullyQualifiedName,
175176
getResolvedSignature: (node, candidatesOutArray, theArgumentCount) => {
176177
node = getParseTreeNode(node, isCallLikeExpression);

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,8 @@ namespace ts {
27392739
getAugmentedPropertiesOfType(type: Type): Symbol[];
27402740
getRootSymbols(symbol: Symbol): Symbol[];
27412741
getContextualType(node: Expression): Type | undefined;
2742+
/* @internal */ isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElementLike | JsxAttributeLike): boolean;
2743+
27422744
/**
27432745
* returns unknownSignature in the case of an error.
27442746
* @param argumentCount Apparent number of arguments, passed in case of a possibly incomplete call. This should come from an ArgumentListInfo. See `signatureHelp.ts`.

src/services/refactors/extractSymbol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ namespace ts.refactor.extractSymbol {
10041004
const localNameText = getUniqueName(isClassLike(scope) ? "newProperty" : "newLocal", file.text);
10051005
const isJS = isInJavaScriptFile(scope);
10061006

1007-
const variableType = isJS
1007+
const variableType = isJS || !checker.isContextSensitive(node)
10081008
? undefined
10091009
: checker.typeToTypeNode(checker.getContextualType(node), scope, NodeBuilderFlags.NoTruncation);
10101010

tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition7.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const i = 0;
1515
class C {
1616
M() {
1717
for (let j = 0; j < 10; j++) {
18-
const newLocal: any = i + 1;
18+
const newLocal = i + 1;
1919
x = /*RENAME*/newLocal;
2020
}
2121
}
@@ -25,7 +25,7 @@ class C {
2525

2626
const i = 0;
2727
class C {
28-
private readonly newProperty: any = i + 1;
28+
private readonly newProperty = i + 1;
2929

3030
M() {
3131
for (let j = 0; j < 10; j++) {
@@ -37,7 +37,7 @@ class C {
3737
// ==SCOPE::Extract to constant in global scope==
3838

3939
const i = 0;
40-
const newLocal: any = i + 1;
40+
const newLocal = i + 1;
4141
class C {
4242
M() {
4343
for (let j = 0; j < 10; j++) {

0 commit comments

Comments
 (0)