Skip to content

Rename string literal values #15370

@maxime1992

Description

@maxime1992

I have interfaces in my app.

Let's take a dog as example :

interface Dog {
  name: string
  id?: number
}

When I post a Dog on the server, I ask the user to give me the dog's name and the server will return an object of Dog type.

So my function to post the Dog only needs his name :

function postDog(name: string) {
   // ...
}

const dog: Dog = { name: 'wouaf' }
postDog(dog.name)

The thing is, if I decide to rename the Dog's name property, my function won't have the parameter renamed. (yes this is pure luxury 💎 and it wouldn't break anything in frontend at least because if my interface matches what the server is expecting ...)

So a collegue (@vnoel) helped me figure that out :

// utility
type Prop<T, K extends keyof T> = { [P in K]: T[P] }

type Doggy = Prop<Dog, 'name' | 'id'>

function f(o: Doggy) {
  console.log(o.id, o.name)
}

f({
  id: 5,
  name: 'wouaf'
})

This is great ! The only thing is that when I use F2 to rename the property (in VSC) :
image

Would it be possible to auto-rename the property everywhere ?
At least we have an error which is great for now :) But doing that automatically would be 🥇

PS : I think it's related to #7611 and #9257 but a little bit different

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions