diff --git a/src/harness/unittests/extractMethods.ts b/src/harness/unittests/extractMethods.ts index bfd7519449d..86ee5354a5c 100644 --- a/src/harness/unittests/extractMethods.ts +++ b/src/harness/unittests/extractMethods.ts @@ -728,6 +728,11 @@ function parseUnaryExpression(operator: string): UnaryExpression { function parsePrimaryExpression(): any { throw "Not implemented"; +}`); + // Type parameter as declared type + testExtractMethod("extractMethod30", + `function F() { + [#|let t: T;|] }`); }); diff --git a/src/services/refactors/extractMethod.ts b/src/services/refactors/extractMethod.ts index 5749c2b71d5..eed49524656 100644 --- a/src/services/refactors/extractMethod.ts +++ b/src/services/refactors/extractMethod.ts @@ -1222,7 +1222,11 @@ namespace ts.refactor.extractMethod { substitutionsPerScope[i].set(symbolId, substitution); } else if (isTypeName) { - errorsPerScope[i].push(createDiagnosticForNode(identifier, Messages.TypeWillNotBeVisibleInTheNewScope)); + // If the symbol is a type parameter that won't be in scope, we'll pass it as a type argument + // so there's no problem. + if (!(symbol.flags & SymbolFlags.TypeParameter)) { + errorsPerScope[i].push(createDiagnosticForNode(identifier, Messages.TypeWillNotBeVisibleInTheNewScope)); + } } else { usagesPerScope[i].usages.set(identifier.text as string, { usage, symbol, node: identifier }); diff --git a/tests/baselines/reference/extractMethod/extractMethod30.ts b/tests/baselines/reference/extractMethod/extractMethod30.ts new file mode 100644 index 00000000000..67dc1208abc --- /dev/null +++ b/tests/baselines/reference/extractMethod/extractMethod30.ts @@ -0,0 +1,19 @@ +// ==ORIGINAL== +function F() { + let t: T; +} +// ==SCOPE::inner function in function 'F'== +function F() { + /*RENAME*/newFunction(); + + function newFunction() { + let t: T; + } +} +// ==SCOPE::function in global scope== +function F() { + /*RENAME*/newFunction(); +} +function newFunction() { + let t: T; +}