In visual studio (or other common javascript/typescript IDEs), is there a simple way to rename properties in a class and automatically rename them in other places of the code where they are not in dot-notation as well?
The issue appears e.g. when properties are accessed with bracket notation obj["prop"]
instead of obj.prop
or when listing subsets of property names for some reason:
class C {
constructor(
public name: string,
public age: number,
public id: string
){}
};
let obj = new C("bob", 30, "abcdef");
let interesting = ["name", "age"];
console.log(`${interesting[0]}: ${obj[interesting[0]]}, ${interesting[1]}: ${obj[interesting[1]]}`);
I don't know of a good way to associate such strings with property names (apart from typing the strings as keyof C
, which would at least give a compiler error after renaming) or IDEs smart enough to understand the relation.
There was the fancy idea of transforming everything into dot notation, with a method taken from a related question - namely converting a dummy function containing only an unused statement of the property access to string and parsing that. Then the common IDEs would rename it aswell. However, this introduces code which is completely unnecessary for the application itself and is not very robust unless written with extreme care - as e.g. Function.toString
is partly implementation dependant.
Is there no practical way except doing most of it manually?