microsoft / microsoft/TypeScript
Error "'this' implicitly has type 'any'" when used .bind()
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
› tsc --version
Version 2.7.0-dev.20171020
Code
tsconfig.json:
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"strict": true
},
"files": [
"index.d.ts",
"index.js"
]
}
index.js:
/** @type {MyObj} */
const o = {
foo: function() {
(function() {
console.log(this); // <- Unexpected error here.
}.bind(this))();
}
};
index.d.ts:
interface MyObj {
foo(this: { a: number }): void;
}
How it looks in the editor:
Context of foo() is defined:

But context passed to the nested function is lost:

Expected behavior:
There should not be error, because this explicitly specified by .bind().
Actual behavior:
› tsc
index.js(5,25): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
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 damit, die Diagnose mit der bereitgestellten tsconfig.json, index.js und index.d.ts unter Verwendung des gemeldeten tsc-Setups zu reproduzieren. Verfolge, wie die JavaScript-Prüfung this für die verschachtelte Funktion und .bind(this) auflöst; fertig ist die Aufgabe, wenn das Beispiel nicht mehr TS2683 meldet und das verwandte Verhalten weiterhin durch Tests abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100