Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function's attached JSDoc is not preserved when using OmitThisParameter #54783

Open
zaygraveyard opened this issue Jun 26, 2023 · 5 comments
Open
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@zaygraveyard
Copy link

zaygraveyard commented Jun 26, 2023

Bug Report

🔎 Search Terms

JSDoc, types with docs, preserve docs, OmitThisParameter, bind, inherit description

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Something to do with this" and "Strong typing of Function members call/bind/apply"

⏯ Playground Link

Playground link with relevant code

💻 Code

/**
 * This description is lost
 * 
 * @param p As well as this decription
 */
function func(this: {}, p: string): void {}

const f = func as OmitThisParameter<typeof func>;
const g = func.bind({});

🙁 Actual behavior

Hovering over f (or g) shows the type but not the descriptions that were attached to func.

🙂 Expected behavior

f and g to inherit the descriptions of func.

Clarification

The problem mainly lies with OmitThisParameter as bind's declaration uses it.
I'm also not sure if this is considered a bug or a missing feature, so I apologize in advance if this is not the right way to report it.

Might be related to #50715

@RyanCavanaugh
Copy link
Member

I'd say this is definitely a "missing feature". For any arbitrary type definition

type X<T, U> = // whatever

there aren't any rules for whether JS Doc should show what (if anything) is on X, T, U, or some combination thereof.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jun 28, 2023
@zaygraveyard
Copy link
Author

Although the general case would be awesome to have, it would be non-trivial to determine its resulting JS Doc.
In contrast to the limited case of OmitThisParameter, retaining the same JS Doc (and possibly removing the @this).
Correct me if I'm wrong but I feel the latter would be much simpler to implement than the former.

@fatcerberus
Copy link

It isn’t though because OmitThisParameter (TIL this exists) is implemented in userland, so it reduces to the much more difficult general case.

@zaygraveyard
Copy link
Author

It can be changed into an intrinsic (like Uppercase, Lowercase, ...), or a new intrinsic can be added that preserves the JS Doc.
Alternatively, a marker type OmitThisType (analogous to ThisType) can be added. Which would be sufficient for my use case (of mapping an object of methods into an object of bound methods).

I would understand if this isn't a path the TypeScript team would like to take 😅

@zaygraveyard
Copy link
Author

It looks like my test case above was misleading as it depends on having assignments that preserve the docs (which I believe is not supported).

I think the following code better demonstrates the issue:

const F = {
  /**
   * This description is lost
   */
  foo(this: string): void {},
};

const a: { [K in keyof typeof F]: OmitThisParameter<typeof F[K]> } = {} as any;
const b: { [K in 'foo']: OmitThisParameter<typeof F[K]> } = {} as any;
const c: { foo: OmitThisParameter<typeof F['foo']> } = {} as any;

a.foo(); // <- doc preserved ✅
b.foo(); // <- doc NOT preserved ❌
c.foo(); // <- doc NOT preserved ❌

⏯ Playground Link

Why is the description preserved in the case of a but not in case of the others b and c?

Note: The description is preserved in all three cases when not using OmitThisParameter or not specifying a this parameter type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants