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

String Literal refactoring support #5602

Closed
basarat opened this issue Nov 11, 2015 · 14 comments · Fixed by #39298
Closed

String Literal refactoring support #5602

basarat opened this issue Nov 11, 2015 · 14 comments · Fixed by #39298
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@basarat
Copy link
Contributor

basarat commented Nov 11, 2015

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)

@DanielRosenwasser DanielRosenwasser added the Suggestion An idea for TypeScript label Nov 11, 2015
@falsandtru
Copy link
Contributor

+1

@mhegazy mhegazy added Help Wanted You can do this Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". labels Nov 11, 2015
@mhegazy
Copy link
Contributor

mhegazy commented 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
Copy link
Member

so we will need to check their contextual type

This will require a breaking change in the type checker API because right now the exposed getContextualType returns the apparent type.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 11, 2015

ah. we can add another one, to return the string literal type at a location then.

@DanielRosenwasser
Copy link
Member

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.

@mhegazy mhegazy assigned ghost Sep 21, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Sep 21, 2016
@mhegazy mhegazy added Bug A bug in TypeScript and removed Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Suggestion An idea for TypeScript labels Sep 21, 2016
@mhegazy mhegazy modified the milestones: TypeScript 2.1, Future Sep 29, 2016
@ZSkycat
Copy link

ZSkycat commented Aug 30, 2018

another case:

interface TestA {
    'propA': number;
}

type TestAKey = keyof TestA;

function FnA<K extends TestAKey>(type: K, val: TestA[K]) {}

FnA('propA', 0);

Support for renaming refactoring will be useful for functions like addEventListener.

@Duckers
Copy link

Duckers commented 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
Copy link

Niryo commented 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
Copy link
Contributor

fuafa commented Oct 2, 2019

Maybe IntelliJ implements their own language service?

@Niryo
Copy link

Niryo commented 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
Copy link

That would be very handy to have such feature in TypeScript

@dontsave
Copy link

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
Copy link

theluk commented Jun 4, 2020

mega bump

@Kingwl
Copy link
Contributor

Kingwl commented Jun 23, 2020

I'd like to take a try on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.