Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Zzzen opened this issue Aug 6, 2020 · 1 comment · Fixed by #41992
Closed

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

Zzzen opened this issue Aug 6, 2020 · 1 comment · Fixed by #41992
Labels
Bug A bug in TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol
Milestone

Comments

@Zzzen
Copy link
Contributor

Zzzen commented Aug 6, 2020

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

@Zzzen
Copy link
Contributor Author

Zzzen commented Aug 15, 2020

webstorm is working as expected.

Large GIF (2038x944)

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Sep 2, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 2, 2020
@RyanCavanaugh RyanCavanaugh added the Domain: Refactorings e.g. extract to constant or function, rename symbol label Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants