Skip to content

Renaming interface members and object shorthand property names #12007

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

Closed
normalser opened this issue Nov 2, 2016 · 13 comments
Closed

Renaming interface members and object shorthand property names #12007

normalser opened this issue Nov 2, 2016 · 13 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@normalser
Copy link

normalser commented Nov 2, 2016

TypeScript Version: nightly (2.1.0-dev.20161102)

Code

interface Test {
    foo: string
}


function bar(): Test {
    const foo = 'foo'
    return { foo }
}

Rename foo on line 2 to foo2

Expected behavior:

interface Test {
    foo2: string
}


function bar(): Test {
    const foo = 'foo'
    return { foo2: foo }
}

Actual behavior:

interface Test {
    foo2: string
}


function bar(): Test {
    const foo = 'foo'
    return { foo2 } // <------- broken code
}
@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2016

the problem is the RenameLocation interface returns to the IDE/Editor a list of rename locations; here you would need to send a list of edits as well. i.e. "foo" => "foo2: foo".

@normalser normalser added Suggestion An idea for TypeScript API Relates to the public API for TypeScript labels Nov 7, 2016
@normalser
Copy link
Author

Then maybe this would be easier and still better:

interface Test {
    foo2: string
}


function bar(): Test {
    const foo2 = 'foo'  // <---- rename variable as well, so that next line is not broken
    return { foo2 }
}

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2016

that might be easier.

@ivan7237d
Copy link

Renaming variable wouldn't always work, right? Because if you rename only the foo in Test1:

interface Test1 {
  foo: string;
}

interface Test2 {
  foo: string;
}

function bar(): [Test1, Test2] {
  const foo = 'foo';
  return [{ foo }, { foo }];
}

this should end up looking not like this

interface Test1 {
  foo2: string;
}

interface Test2 {
  foo: string;
}

function bar(): [Test1, Test2] {
  const foo2 = 'foo';
  return [{ foo2 }, { foo2 }];
}

but like this:

interface Test1 {
  foo2: string;
}

interface Test2 {
  foo: string;
}

function bar(): [Test1, Test2] {
  const foo = 'foo';
  return [{ foo2: foo }, { foo }];
}

I thought renaming was pretty safe until I came across this issue... but on the bright side, typechecks should normally catch the errors.

@dgreensp
Copy link

Because of this bug, renaming an interface member typically produces incorrect code, from what I've seen. It's almost enough to make me mistrust the "rename symbol" feature... but is this basically the only glaring bug, in people's experience?

@clarkio
Copy link

clarkio commented Jun 14, 2018

Adding @TimFerrell who also shared this feedback which shows up in Visual Studio Code

@TimFerrell
Copy link

Correct, this issue applies to Javascript as well.

@clarkio
Copy link

clarkio commented Jun 14, 2018

Yes and the refactoring in VS code is driven by TypeScript under the hood. I think once this issue is addressed here in TypeScript it will also address the similar issue you noticed around the refactor features in the editor.

@thorn0
Copy link

thorn0 commented Jul 18, 2018

IMHO. this issue should be labeled as a bug.

@zurfluh
Copy link

zurfluh commented Aug 15, 2018

I stumbled a few times because of this bug. I have seen bugs in our code because of this. I would appreciate this being fixed.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed API Relates to the public API for TypeScript Suggestion An idea for TypeScript labels Aug 15, 2018
@RyanCavanaugh RyanCavanaugh modified the milestones: Future, TypeScript 3.2 Aug 15, 2018
@ghost ghost added the Fixed A PR has been merged for this issue label Sep 26, 2018
@ghost ghost closed this as completed in #27356 Sep 26, 2018
@tmoran-stenoa
Copy link

It seems the bug is back on the latest VSCode version (Typescript 5.5.4):

Screen.Recording.2024-12-23.at.10.52.37.PM.mov

"About VSCode" details:

Version: 1.96.1 (Universal)
Commit: 42b266171e51a016313f47d0c48aca9295b9cbb2
Date: 2024-12-17T17:50:05.206Z
Electron: 32.2.6
ElectronBuildId: 10629634
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Darwin arm64 23.5.0

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jan 2, 2025

@tmoran-stenoa likely you have the "Use aliases for renames" option set wrong

Image

@tmoran-stenoa
Copy link

@RyanCavanaugh You're right, it was off, works well now that it's enabled. Thank you so much!!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

10 participants