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

Rename function / find all references doesn't affect inline object literals #282

Closed
fsoikin opened this issue Jul 28, 2014 · 3 comments
Closed
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@fsoikin
Copy link

fsoikin commented Jul 28, 2014

Check out this code:

interface I {
    X?: number;
    Y?: string;
}

var i: I;
i.X = 5;

function f() {
    return <I> { X: 1, Y: 'a' };
}

(playground link)

Notice how the I.X member is referenced two times - once in the i.X assignment and once in the object literal inside function f(). Now, if I put my caret on the X definition and hit F2, the definition itself and the i.X assignment get renamed, - but the object literal remains as it was. Worse still, because X is an optional property, the compiler doesn't catch the rename at compile time either, and I end up with a good old JavaScript typo situation.

I do realize that it is not always possible for the compiler to know if an object literal is intended to represent an interface or not. For example:

interface I {
    X?: number;
}
interface J {
    X?: number;
    Y?: string;
}

var i: I = f();
var j: J = f();

function f() {
    return { X: 1 }
}

However, I am willing to go an extra mile to help the compiler out here. That's why I put the <I> annotation on the object literal in the first example. The problem, however, is that it's impossible for me to lend the compiler a hand, even when I want to.

This is a very real and painful problem when working with property bags, which is a very common JS pattern.

@RyanCavanaugh RyanCavanaugh changed the title Rename function doesn't affect inline object literals Rename function / find all references doesn't affect inline object literals Jul 29, 2014
@RyanCavanaugh
Copy link
Member

We've definitely wanted to fix this for a while and a minimal implementation shouldn't require any major design (just use contextual typing rules). Let's try to get sign-off on the straightforward cases at least.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Apr 27, 2015
@RyanCavanaugh
Copy link
Member

Not really anything tricky here, we should just do this

@mhegazy
Copy link
Contributor

mhegazy commented May 7, 2015

Looks like this is fixed to me :)

rename

@mhegazy mhegazy closed this as completed May 7, 2015
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 7, 2015
@mhegazy mhegazy added this to the TypeScript 1.5 milestone May 7, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants