microsoft / microsoft/TypeScript
Inconsistent `typeof this` when the `this` parameter declared in the signature of a function parameter
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
🔎 Search Terms
"this parameter", "typeof this"
🕗 Version & Regression Information
- This changed between versions 5.9.3 and 6.0.x
⏯ Playground Link
💻 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,
}
}
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der verlinkten TypeScript Playground-Reproduktion und vergleiche das Verhalten zwischen 5.9.3 und 6.0.3, mit Schwerpunkt auf der kontextuellen Typisierung von Callback-this-Parametern. Finde die Compiler-Tests für die Inferenz des this-Typs und füge einen Regressionstest hinzu, der die drei Beispiele mit Objektmethoden und den eigenständigen Callback abdeckt; abgeschlossen ist die Aufgabe, wenn typeof this konsistent zu A aufgelöst wird, ohne eine Verwendung von this zur Laufzeit zu erfordern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100