microsoft / microsoft/TypeScript
instanceof AbstractClass.constructor should narrow to typeof AbstracClass
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Please try to reproduce the issue with
typescript@next. It may have already been fixed.
TypeScript Version: 3.3.0-dev.20181208
Search Terms: static instanceof constructor
Code
type Meta = {foo: string};
abstract class Abstract {
static meta: Meta;
public abstract toString(): string;
}
const getMeta = (fromModule: any): Meta | undefined => {
return fromModule instanceof Abstract.constructor
? fromModule.meta : undefined;
};
Expected behavior:
Narrow the type of fromModule to typeof Abstract, so that static fields can be accessed.
(In Typescript versions <= 3.1.6 the above code didn't raise an error, my guess is that the type was not narrowed at all)
Actual behavior:
The narrowed type of fromModule is {}:
Error:(10, 18) TS2339: Property 'meta' does not exist on type '{}'.
Related Issues:
I found the following potentially related issues but they didn't seem to be an exact match or to old (because this problem only exists since 3.2.1):
- #27276
- #23274
- #16035
Workaround
Creating a type guard is of course doable for a single abstract class.
const extendsAbstract = (FromModule: unknown): FromModule is typeof Abstract =>
FromModule && FromModule instanceof Abstract.constructor;
(Or maybe it could even be written in a generic way?)
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, das Beispiel im verlinkten TypeScript Playground mit typescript@next zu reproduzieren, und vergleiche dann das Verhalten mit der gemeldeten Version 3.3.0-dev.20181208. Prüfe zuerst die verwandten Issues #27276, #23274 und #16035, bevor du das instanceof-Narrowing-Verhalten des Compilers nachverfolgst. Als abgeschlossen gilt die Aufgabe, wenn das Beispiel fromModule auf typeof Abstract eingrenzt, sodass das statische Feld meta akzeptiert wird, und eine Abdeckung für die Regression vorhanden ist.
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
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100