microsoft / microsoft/TypeScript

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

Ouverte
#44,461 2 commentaires 1 réaction 1 personne assignée Voir sur GitHub

@sandersn y travaille déjà.

Depuis le 10/6/2021.

Needs Investigation
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.