Skip to content

String Literal refactoring support #5602

Closed
@basarat

Description

@basarat

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

falsandtru commented on Nov 11, 2015

@falsandtru
Contributor

+1

added
Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".
on Nov 11, 2015
mhegazy

mhegazy commented on Nov 11, 2015

@mhegazy
Contributor

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

DanielRosenwasser commented on Nov 11, 2015

@DanielRosenwasser
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

mhegazy commented on Nov 11, 2015

@mhegazy
Contributor

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

DanielRosenwasser

DanielRosenwasser commented on Nov 11, 2015

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

added this to the TypeScript 2.1 milestone on Sep 21, 2016
added
BugA bug in TypeScript
and removed
Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".
on Sep 21, 2016

6 remaining items

Duckers

Duckers commented on Sep 16, 2019

@Duckers

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

Niryo commented on Oct 1, 2019

@Niryo

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

fuafa commented on Oct 2, 2019

@fuafa
Contributor

Maybe IntelliJ implements their own language service?

Niryo

Niryo commented on Oct 2, 2019

@Niryo

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

aspirisen commented on Oct 8, 2019

@aspirisen

That would be very handy to have such feature in TypeScript

dontsave

dontsave commented on Jan 10, 2020

@dontsave

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

theluk commented on Jun 4, 2020

@theluk

mega bump

Kingwl

Kingwl commented on Jun 23, 2020

@Kingwl
Contributor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @theluk@Duckers@basarat@DanielRosenwasser@falsandtru

      Issue actions

        String Literal refactoring support · Issue #5602 · microsoft/TypeScript