microsoft / microsoft/TypeScript

Support either type asserting `self` in classes, or properly ignoring workaround

Aperta
#39,864 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: classes Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

I currently have a scenario very similar to https://github.com/microsoft/TypeScript/issues/2000#issuecomment-546929745. While it would be fixed it TS natively supported overriding class methods (see that issue), I followed the outline of the linked comment, but am running into a small issue. This is not a huge blocker but rather a nice-to have to get proper type completion for my scenario.


export class WritableObjStream<T> extends (stream.Writable as any) {
    writableObjectMode: true

    write(object: T, cb?: (error: Error | null | undefined) => void): boolean {
        return super.write(...more details)
    }
}

Because we type assert the base class to any, TS has no knowledge of the superclass. I tried a few solutions, with errors included below:

(super as any).write() // 'super' must be followed by an argument list or member access.
let s: stream.Writable = undefined;
// @ts-ignore
s = super // Identifier expected.

return s.write() // 'super' must be followed by an argument list or member access, Expected 1-3 arguments, but got 0.

Luckily, it autocomplete is picking up on the proper type of s.write() now and giving me an argument error.
Also, from what I can gather super is treated as a keyword not an identifier, which results in the error above.

This may not be the most common of errors, but there may be a few scenarios/other use cases. I tried writing a few examples up but none made real-world sense besides this use-case of a workaround for overriding methods.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l'esempio di classe nell'issue e con l'issue/il commento di TypeScript collegato che descrive il workaround. Analizza come TypeScript esegue il parsing e il controllo dei tipi di super in questo scenario, quindi stabilisci se il type assertion di self debba essere supportato o se il workaround debba essere rifiutato esplicitamente. Il lavoro è concluso quando il comportamento scelto è documentato da test che coprono gli esempi mostrati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.