Skip to content

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

Closed

Description

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.

Activity

  1. changed the title [-]Rename function doesn't affect inline object literals[/-] [+]Rename function / find all references doesn't affect inline object literals[/+] on Jul 29, 2014
  2. RyanCavanaugh commented on Jul 29, 2014

    @RyanCavanaugh
    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.

  3. RyanCavanaugh commented on Apr 27, 2015

    @RyanCavanaugh
    Member

    Not really anything tricky here, we should just do this

  4. mhegazy commented on May 7, 2015

    @mhegazy
    Contributor

    Looks like this is fixed to me :)

    rename

  5. locked and limited conversation to collaborators on Jun 18, 2018
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 TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions