microsoft / microsoft/TypeScript

Private/protected properties on siblings in union types

Aperta
#9,974 3 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: Error Messages Help Wanted Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

TypeScript Version: 1.8.9

Code

class Example {
    protected width = 0;
    constructor(sibling: Example | HTMLCanvasElement) {
        console.log(sibling.width);
    }
}

Expected behavior: No errors.

Actual behavior: Error: Property 'width' does not exist on type 'Example | HTMLCanvasElement'. Line 4, column 31

The reason this error occurs is that the width property is protected on Example but public on HTMLCanvasElement. However, the error is confusing for two reasons:

  1. The width property is available on both component properties in this situation. If the sibling parameter had a type of either Example or HTMLCanvasElement, the code would compile.
  2. The error message doesn't explain why the property does not exist on the type. Understanding the error requires the knowledge that properties are only available on union types if they have the same privacy level, even if those properties would be accessible in the component types in the active context.

I am not sure if there are situations in which the desired behavior is a compilation error when accessing a potentially protected property on a union type if that property is accessible on all component types. I can't think of any, and so I think the ideal situation would be that the compiler does not throw an error here. However, I think it would be an adequate solution to improve the error message.

(I apologize if this has been reported/discussed before. I searched but couldn't find anything exactly along these lines.)

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 compilando l’esempio TypeScript nell’issue con TypeScript 1.8.9 e conferma la diagnostica della proprietà di unione. Traccia il percorso del checker per l’accesso a width su Example | HTMLCanvasElement, quindi determina se i membri protected/public accessibili debbano essere compilati o ricevere un errore più chiaro. Il lavoro è completato quando il comportamento scelto è implementato e coperto da un test di regressione per questo esempio.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.