microsoft / microsoft/TypeScript

Inconsistent `typeof this` when the `this` parameter declared in the signature of a function parameter

Ouverte
#63,616 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: This-Typing
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔎 Search Terms

"this parameter", "typeof this"

🕗 Version & Regression Information
  • This changed between versions 5.9.3 and 6.0.x
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/JYOwLgpgTgZghgYwgAgILIN7LgLmQZzClAHNkBfAKEo0uWQHoHlRJZEUAhTZEgCjAALYPjyoAlHgBuAe2AATCnUbMAesgDKqALIBRZAAUASgHlOAGV3blraPCTJuWEngHDRaccgC8APmSyCkrKMACuIAhgwDIgyDB8APp4nJIBcooYVCF8tPT0-F65efRgAJ4ADigAGj7IZZUyMHXuysVMeercfPiCMqEANooARigSrUr05OLU9PFF+XyF4yUtxSUV1bX1EI3NIssqHWjjVJPT2fO8i5gH28g13nUbu0L7a4f06qi3q3mnFNMskUwhEojE4olXK8PBIfP5AvJUgjMFlZnwQZForECjc2swACobZDQKAyKCuABMADYABwAZi8AHJoYyWABbcr9YAIYBgfqlZCCOD4J6VZCMuAgUqskYIOChfAoXnIeQyCAikAyMCCuBSFBwUX6kCasBwMEgAB043aAAEwPgALQQAAelUiTqgpKg4zuD0NL1+UxmEIx5quS2K0J9RL92wDbzy7U+xzOwfioax4dxeV9W2eTSjeKO30jgcBlCAA

💻 Code
interface A { a: string }

{
  // interface B { g(this: A): void }
  // ^ SAME PROBLEM
  interface B { g: (this: A) => void }

  function f(_: B): void {}

  f({
    g() {
      type X = typeof this
      //   ^ B (should be A)
    }
  })

  f({
    g() {
      this
      type X = typeof this
      //   ^ A
    }
  })

  f({
    g() {
      type X = typeof this
      //   ^ A
      this
    }
  })
}

{
  function f(_: (this: A) => void): void {}

  f(function g() {
    // Type error: (2683) 'this' implicitly has type 'any' because it does not have a type annotation.
    // @ts-expect-error
    type X = typeof this
  })

  f(function g() {
    this
    type X = typeof this
    //   ^ A
  })

  f(function g() {
    type X = typeof this
    //   ^ A
    this
  })
}
🙁 Actual behavior

When declaring the this parameter in the signature of a function parameter (e.g., a callback function like function f(callback: (this: A) => void): void {}), the inferred typeof this in the function body can be inconsistent. If this is used in the function body, typeof this behaves as expected. If this is not used, typeof this is inferred as if the this parameter was never declared in the signature.

🙂 Expected behavior

The inferred typeof this should be consistent regardless of the usage of this in the function body.

Additional information about the issue

I can reproduce this problem also in @typescript/native-preview: 7.0.0-dev.20260705.1.

Locally I used the "default" tsconfig.json (tsc --init/tsgo --init):

{
  // Visit https://aka.ms/tsconfig to read more about this file
  "compilerOptions": {
    // Environment Settings
    // See also https://aka.ms/tsconfig/module
    "module": "nodenext",
    "target": "esnext",
    "types": [],

    // Other Outputs
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,

    // Stricter Typechecking Options
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,

    // Recommended Options
    "strict": true,
    "jsx": "react-jsx",
    "verbatimModuleSyntax": true,
    "isolatedModules": true,
    "noUncheckedSideEffectImports": true,
    "moduleDetection": "force",
    "skipLibCheck": true,
  }
}

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.

Piste de recherche

Commencez par la reproduction liée dans TypeScript Playground et comparez le comportement entre 5.9.3 et 6.0.3, en vous concentrant sur le typage contextuel des paramètres this des callbacks. Localisez les tests du compilateur pour l’inférence du type this et ajoutez un cas de régression couvrant les trois exemples de méthodes d’objet et le callback autonome ; le travail est terminé lorsque typeof this est systématiquement résolu en A sans nécessiter une utilisation de this à l’exécution.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

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