microsoft / microsoft/TypeScript
Private/protected properties on siblings in union types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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:
- The
widthproperty is available on both component properties in this situation. If thesiblingparameter had a type of eitherExampleorHTMLCanvasElement, the code would compile. - 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.)
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
Beginnen Sie damit, das TypeScript-Beispiel im Issue mit TypeScript 1.8.9 zu kompilieren, und bestätigen Sie die Diagnose zur Union-Eigenschaft. Verfolgen Sie den Checker-Pfad für den Zugriff auf width bei Example | HTMLCanvasElement, und bestimmen Sie anschließend, ob die zugänglichen protected/public-Mitglieder kompiliert werden sollten oder einen klareren Fehler erhalten sollten. Als abgeschlossen gilt die Aufgabe, wenn das ausgewählte Verhalten implementiert und durch einen Regressionstest für dieses Beispiel abgedeckt 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