Skip to content

Rename and fine all references of Typescript's string literal values  #7611

Closed

Description

From Ziv (@zivni) on March 20, 2016 8:40

Type script string literals allow to have a code such as

//interfaces.d.ts
type Options = "option 1 " | "option 2" 

//file.ts
let myOption: Options = "option 1" // "option 3" will give us an error, right in the IDE.

It would be nice to be able to rename and find all references of a specific value. e.g. find/rename all occurrences of "option 1". Currently we can only find the references of the type itself.

Copied from original issue: microsoft/vscode#4469

Activity

  1. mhegazy commented on Apr 29, 2016

    @mhegazy
    Contributor

    so should we rename all literal types "option 1" or just the ones from this declaration?

    for example:

    type Options = "option 1" | "option 2"
    let myOption: Options = "option 1" // rename here,
    
    function foo(a: "option 1") { } // should this be renamed as well?
  2. added 2 commits that reference this issue on Apr 29, 2016
    5961984
    423503c
  3. added a commit that references this issue on May 2, 2016
    afaffb1
  4. zivni commented on Nov 17, 2016

    @zivni

    This does not work as accepted. I don't know if this problem is with Typescript or with VSCode.

    Just open a new test.ts file with this content in vscode:

    type A= "aaa" | "article" 
    type B = "aaa" | "ccc"
    
    var a:A="aaa"
    var b:B="aaa"
    

    press F2 to rename the "aaa" of the A type. the "aaa" of the B type is also renamed.
    It goes even further, rename the "article" of the A type. You'll notice that it changed the typing in lib.es6.d.ts togetElementsByTagName(tagname: "renamed_article"): NodeListOf<HTMLElement>;

    And also the find all references does not work

    Tried it on vscode 1.7.1 on windows.

  5. mhegazy commented on Nov 18, 2016

    @mhegazy
    Contributor

    there is really no difference between "aaa" in type A and in type B. they are really the same type. the lib.d.ts change should be disabled, this is tracked by #7458

  6. zivni commented on Nov 18, 2016

    @zivni

    I will have to disagree.
    It like saying that 2 properties with the same name in two different interfaces are the same.

    Lets take a more concrete example: As typescript doesn't yet have a nameof operator, a common practice, when you need to pass a member name to a function, is to list members using a string literal types. E.g.:

    type UserMembers = "id" | "name | ...
    interface User{
       id: string
       name: string
       //...other properties
    }
    
    type OrderMembers = "id" | "name | ...
    interface Order{
       id: string
       name: string
       //...other properties
    }
    
    // and then:
    const orderByFields:UserMemebers[] = ["name","id"]
    let orderedUsers=_.orderBy(users,orderByFields)
    

    No lets say we decide to rename the members to more meaningful names, like userId and orderId, or userName and orderName. The interfaces will be renamed as accepted, but the string literal types will not.

    Another example could be tat each have a status member with there own string literal types. and you need to change just a status name og just one of them

    Also the problem described in #7458 is a different problem. there the rename is of a specific member, and what was wanted is to rename a specific instance of "html" code. Here the problem with article is that it renames something that is entirely unconnected to the renamed object.

  7. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions