Created
July 30, 2024 12:24
-
-
Save lubieowoce/dd41c13c764235ae252d8771e211d029 to your computer and use it in GitHub Desktop.
Change type of object property while preserving JSDoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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