diff --git a/src/services/codefixes/fixImplicitThis.ts b/src/services/codefixes/fixImplicitThis.ts index 743db5c7f35..a373c283052 100644 --- a/src/services/codefixes/fixImplicitThis.ts +++ b/src/services/codefixes/fixImplicitThis.ts @@ -4,7 +4,7 @@ namespace ts.codefix { const errorCodes = [Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation.code]; registerCodeFix({ errorCodes, - getCodeActions(context) { + getCodeActions: function getCodeActionsToFixImplicitThis(context) { const { sourceFile, program, span } = context; let diagnostic: DiagnosticAndArguments | undefined; const changes = textChanges.ChangeTracker.with(context, t => { @@ -20,7 +20,7 @@ namespace ts.codefix { function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number, checker: TypeChecker): DiagnosticAndArguments | undefined { const token = getTokenAtPosition(sourceFile, pos); - Debug.assert(token.kind === SyntaxKind.ThisKeyword); + if (!isThis(token)) return undefined; const fn = getThisContainer(token, /*includeArrowFunctions*/ false); if (!isFunctionDeclaration(fn) && !isFunctionExpression(fn)) return undefined; diff --git a/tests/cases/fourslash/codeFixImplicitThis_ts_functionDeclarationInGlobalScope.ts b/tests/cases/fourslash/codeFixImplicitThis_ts_functionDeclarationInGlobalScope.ts new file mode 100644 index 00000000000..a2d51ba8611 --- /dev/null +++ b/tests/cases/fourslash/codeFixImplicitThis_ts_functionDeclarationInGlobalScope.ts @@ -0,0 +1,12 @@ +/// + +// @noImplicitThis: true + +////function foo() { +//// let x: typeof /**/this; +////} + +verify.codeFixAvailable([ + { description: "Infer 'this' type of 'foo' from usage" }, + { description: "Remove unused declaration for: 'x'" } +]);