microsoft / microsoft/TypeScript

JSDoc @this does not propagate to the actual type of a function

Aperta
#44,461 2 commenti 1 reazione 1 assegnatario Vedi su GitHub

@sandersn ci sta già lavorando.

Dal 10/6/2021.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

When using @this to annotate a function, the underlying type of that function does not reflect the TypeScript equivalent properly when used as a type elsewhere.

🔎 Search Terms

function this jsdoc

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about JSDoc
⏯ Playground Link

Playground link with relevant code

💻 Code

You should test this out in VSCode, as the Play link appears to handle a few things incorrectly.

// Note: hovering over `someCallback` shows in annotation that
// the type of `this` is *, which is weird
// Also note a secondary bug that `someCallback` is assumed to be
// a constructor, even though it isn't, and there is no way
// to override it.
/**
 * @this {HTMLElement}
 * @param {MouseEvent} e
 */
function someCallback(e)
{
    this.textContent = 'hello';
}

// Note: this does not seem to work in Play, but VSCode
// seems to handle @type properly here. The only issue is
// that someCallback does not seem to have a proper `this` type.
/** @type {typeof someCallback} */
function equivalentType(e)
{
    // `this` is "any". In VSCode, `e` has the correct type,
    // but `this` is untyped.
    someCallback.call(this, e);
}

// This should be equivalent to the function above & to someCallback, but is not.
// This is the correct type for `typeof someCallback`, however.
/** @type {(this: HTMLElement, e: MouseEvent) => void} */
function actualEquivalentType(e)
{
    // "this" is typed properly
    someCallback.call(this, e);
}
🙁 Actual behavior

typeof someCallback does not include the this JSDoc component, and is typed as (e: MouseEvent) => void

🙂 Expected behavior

typeof someCallback should be (this: HTMLElement, e: MouseEvent) => void

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.