Closed
Description
Thanks for #5185 🌹. Would be great if we could get refactoring support for string literals
type Foo = "aReallyLong" | "bReallyLong"; // allow me to rename any of these values
let foo:Foo;
foo = "aReallyLong"; // allow me to rename this value
refs #5554 (comment)
Activity
falsandtru commentedon Nov 11, 2015
+1
mhegazy commentedon Nov 11, 2015
PRs are welcomed.
The change should be done in services.ts::getReferencedSymbolsForNode. String literals do not have a symbol, so we will need to check their contextual type, and if it is a string literal type, replace all strings with the same contextual type.
DanielRosenwasser commentedon Nov 11, 2015
This will require a breaking change in the type checker API because right now the exposed
getContextualType
returns the apparent type.mhegazy commentedon Nov 11, 2015
ah. we can add another one, to return the string literal type at a location then.
DanielRosenwasser commentedon Nov 11, 2015
I think we should just do the correct thing and make the break. I don't think most people will be affected by it, and It's going to be necessary elsewhere. We should favor a more general solution that gets users where they need to be than one that solves a specific use-case. I'll make the change and see what breaks in our language service.
6 remaining items
Duckers commentedon Sep 16, 2019
Bump. Perhaps this wasn't that urgent in 2015 when filed, but now with the level of sophistication TS has reached I think this should be reconsidered for roadmap.
Whether this feature exists or not is a pretty big deal wrt. what should be considered best practice for enums when writing new code/new APIs.
String literals have benefits over enums as they require no imports to be used, is easy to debug and serialize, and makes for less verbose code. Also a critical tool for javascript interop where there are no pre-existing enum objects.
Without refactoring support, committing to string literals as best practice for how we design APIs in TypeScript is sketchy.
Niryo commentedon Oct 1, 2019
I don't get something...It looks like Intellij IDE can do exactly what we ask for in this Issue. How the hell Intellij can do this if it's not supported in Typescript itself?
fuafa commentedon Oct 2, 2019
Maybe IntelliJ implements their own language service?
Niryo commentedon Oct 2, 2019
I don't know...Maybe typescript already supports this and it's now an issue with VScode? What about this merged commit #5615, did it solve the issue?
aspirisen commentedon Oct 8, 2019
That would be very handy to have such feature in TypeScript
dontsave commentedon Jan 10, 2020
Bump. This should be a feature in TypeScript and VSCode in 2020. Right now to get refactoring behavior in VSCode you have no choice but to use string enums, which are far less elegant
theluk commentedon Jun 4, 2020
mega bump
Kingwl commentedon Jun 23, 2020
I'd like to take a try on this.