Skip to content

Instantly share code, notes, and snippets.

@lubieowoce
Created July 30, 2024 12:24
Show Gist options
  • Save lubieowoce/dd41c13c764235ae252d8771e211d029 to your computer and use it in GitHub Desktop.
Save lubieowoce/dd41c13c764235ae252d8771e211d029 to your computer and use it in GitHub Desktop.
Change type of object property while preserving JSDoc
type Replace<
Obj extends Record<string, any>,
Field extends keyof Obj,
NewType,
> = Reveal<
{ [Key in keyof Obj]: Key extends Field ? NewType : Obj[Key] }
>
type Reveal<Obj extends Record<string, any>> = {
[K in keyof Obj]: Obj[K]
}
// =========================
type X = {
/** This is a foo. */
foo: string
}
type Y = Replace<X, 'foo', number>
declare const y: Y
void y.foo // same JSDoc!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment