microsoft / microsoft/TypeScript
instanceof AbstractClass.constructor should narrow to typeof AbstracClass
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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?)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l'esempio nel TypeScript Playground collegato con typescript@next, quindi confronta il comportamento con la versione segnalata 3.3.0-dev.20181208. Esamina prima le issue correlate #27276, #23274 e #16035, poi traccia il comportamento di narrowing di instanceof del compilatore. Il lavoro è completato quando l'esempio restringe fromModule a typeof Abstract, così che il campo statico meta venga accettato, con una coverage per la regressione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100