Search Terms
type level jsdoc
computed jsdoc
reference jsdoc from other type
Suggestion
Add a utility type Docs<T>, which, given type T = U & Docs<V>, results in a type T with the type of U, but with the JSDoc annotations of V (overriding any U might have). This should also add V's go to definition results to T's (maybe prioritized?).
This could either be implemented as a magic type like ThisType (e.g. type Docs<T> = unknown in lib.d.ts), or as an intrinsic type, as introduced in #40580.
Use Cases
This is useful to preserve context when manipulating types (see example below).
Examples
interfacePerson{id: string;/** * The date this person was born */birthDate: Date;/** * The date this person died; `null` indicates that the person is still alive. */deathDate: Date|null;/** * This person's age in milliseconds. Calculated as `deathDate - birthDate` * when dead (`deathDate === null`), `currentDate - birthDate` when not. */age: number;name: string;}constalice: Person={id: "12345",birthDate: newDate(),deathDate: null,age: 0,name: "Alice"};alice.deathDate// Hover `deathDate`; we can clearly see what this field means and why it might be nulltypeLazy<T>={[KinkeyofT&stringas `get${Capitalize<K>}`]: ()=>Promise<T[K]>// Proposed syntax:// [K in keyof T & string as `get${Capitalize<K>}`]: (() => Promise<T[K]>) & Docs<T[K]>}typeLazyPerson=Lazy<Person>;declareconstgetLazyPerson: (id: string)=>LazyPerson;constbob=getLazyPerson("1111");bob.getDeathDate// Hover `getDeathDate`; we've lost all information about what this field means. Why could it be null?Playground Link
Checklist
My suggestion meets these guidelines:
Search Terms
type level jsdoc
computed jsdoc
reference jsdoc from other type
Suggestion
Add a utility type
Docs<T>, which, giventype T = U & Docs<V>, results in a typeTwith the type ofU, but with the JSDoc annotations ofV(overriding anyUmight have). This should also addV's go to definition results toT's (maybe prioritized?).This could either be implemented as a magic type like
ThisType(e.g.type Docs<T> = unknowninlib.d.ts), or as anintrinsictype, as introduced in #40580.Use Cases
This is useful to preserve context when manipulating types (see example below).
Examples
Playground Link
Checklist
My suggestion meets these guidelines: