diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ca73d92e0c0..ec591b28db9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -171,6 +171,7 @@ namespace ts { node = getParseTreeNode(node, isExpression); return node ? getContextualType(node) : undefined; }, + isContextSensitive, getFullyQualifiedName, getResolvedSignature: (node, candidatesOutArray, theArgumentCount) => { node = getParseTreeNode(node, isCallLikeExpression); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 54f2ebfae42..98e7bf33ad6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2739,6 +2739,8 @@ namespace ts { getAugmentedPropertiesOfType(type: Type): Symbol[]; getRootSymbols(symbol: Symbol): Symbol[]; getContextualType(node: Expression): Type | undefined; + /* @internal */ isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElementLike | JsxAttributeLike): boolean; + /** * returns unknownSignature in the case of an error. * @param argumentCount Apparent number of arguments, passed in case of a possibly incomplete call. This should come from an ArgumentListInfo. See `signatureHelp.ts`. diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index addc569129d..dadd4865ee6 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -1004,7 +1004,7 @@ namespace ts.refactor.extractSymbol { const localNameText = getUniqueName(isClassLike(scope) ? "newProperty" : "newLocal", file.text); const isJS = isInJavaScriptFile(scope); - const variableType = isJS + const variableType = isJS || !checker.isContextSensitive(node) ? undefined : checker.typeToTypeNode(checker.getContextualType(node), scope, NodeBuilderFlags.NoTruncation); diff --git a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition7.ts b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition7.ts index 2c1d849d825..2ebc01938ac 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition7.ts +++ b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition7.ts @@ -15,7 +15,7 @@ const i = 0; class C { M() { for (let j = 0; j < 10; j++) { - const newLocal: any = i + 1; + const newLocal = i + 1; x = /*RENAME*/newLocal; } } @@ -25,7 +25,7 @@ class C { const i = 0; class C { - private readonly newProperty: any = i + 1; + private readonly newProperty = i + 1; M() { for (let j = 0; j < 10; j++) { @@ -37,7 +37,7 @@ class C { // ==SCOPE::Extract to constant in global scope== const i = 0; -const newLocal: any = i + 1; +const newLocal = i + 1; class C { M() { for (let j = 0; j < 10; j++) {