Skip to content

this is not preserved when extracted to function in module scope #39931

Closed

Description

@Zzzen

TypeScript Version: 3.9.6

Search Terms: extract to function, this

Code

function test(this: any, foo: string) {
  console.log(this.name);
  console.log(foo); 
}

Select these two console.log and extract them to function in module scope.

Expected behavior:
this should be passed to newFunction

function test(this: any, foo: string) {
    newFunction(this, foo);
}

function newFunction(context: any, foo: string) {
    console.log(context.name);
    console.log(foo);
}

Actual behavior:
this in newFunction is undefined now.

function test(this: any, foo: string) {
    newFunction(foo);
}

function newFunction(foo: string) {
    console.log(this.name);
    console.log(foo);
}

Playground Link:
Seems Refactoring is not supported on TypeScript Playground.

Related Issues:

function-this

Activity

  1. Zzzen commented on Aug 15, 2020

    @Zzzen
    ContributorAuthor

    webstorm is working as expected.

    Large GIF (2038x944)

  2. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: LS: Refactoringse.g. extract to constant or function, rename symbol

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions