TypeScript Tips: Rename Fields and Preserve JSDoc
- Authors

- Name
- Geeks Kai
- @KaiGeeks
Loading share buttons...
When refactoring TypeScript code, developers often need to rename fields while preserving the associated JSDoc comments. This process can be challenging, as TypeScript doesn't provide a built-in mechanism to automatically transfer JSDoc comments during field renaming. In this article, we'll explore several approaches to tackle this issue, ensuring that your code remains well-documented even after refactoring.
Consider the following TypeScript interface with JSDoc comments:
interface Person {
/** The person's first name */
firstName: string
/** The person's last name */
lastName: string
}
If we want to rename firstName to givenName, we'd like to preserve the JSDoc comment. However, a simple rename operation doesn't automatically carry over the documentation.
The simplest, albeit tedious, approach is to manually copy the JSDoc comment when renaming a field:
interface Person {
/** The person's first name */
givenName: string
/** The person's last name */
lastName: string
}
While this method works, it's prone to human error and can be time-consuming for large codebases.
A more elegant solution involves using the @see JSDoc annotation to reference the original field's documentation:
interface Person {
/** @see Person.firstName */
givenName: string
/** The person's last name */
lastName: string
}
This approach maintains a link to the original documentation, allowing developers to easily trace the field's history.
In some cases, using TypeScript's Pick or other homomorphic mapped types can preserve JSDoc comments:
type PersonWithGivenName = Pick<Person, "lastName"> & {
givenName: Person["firstName"]
}
This method works well when you're creating new types based on existing ones, but it doesn't directly solve the renaming issue within the same interface.
For large-scale refactoring, consider developing custom tooling to automatically handle JSDoc comment preservation during renaming operations. This could involve creating scripts that analyze your codebase and update JSDoc comments accordingly.
The TypeScript team is aware of the challenges surrounding JSDoc preservation during refactoring. There's an open issue (microsoft/TypeScript#50715) discussing potential improvements for preserving JSDoc comments in mapped types. Keep an eye on future TypeScript releases for possible enhancements in this area.
While renaming fields in TypeScript while preserving JSDoc comments isn't straightforward, several workable solutions exist. By employing techniques like the @see annotation, leveraging TypeScript's type system, or developing custom tooling, developers can maintain valuable documentation during refactoring processes. As the TypeScript ecosystem evolves, we can hope for more native support for these operations in the future. Remember, clear and up-to-date documentation is crucial for maintaining large codebases. Taking the time to properly handle JSDoc comments during refactoring will pay dividends in code clarity and maintainability in the long run.
According to the official TypeScript documentation, JSDoc comments play a significant role in type checking and code analysis.