microsoft / microsoft/TypeScript
"Holey" behavior on differently-typed getter/setter properties
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
I was asked to provide an example of separate read/write types that doesn't necessarily use classes, and here's what I ended up finding. 😅
Take the following:
interface Thing {
get name(): string
set name(value: string | null | undefined);
}
function makeThing(): Thing {
let name = "";
return {
get name() {
return name;
},
set name(value) {
name = value.toString();
}
}
}
In this example, you might think that you're writing type-safe code, but in fact you've got a bug. Our get/set accessors don't have type annotations, so TypeScript assumes the name setter has the type string. Because only the reading type of the name property is validated against Thing, TypeScript thinks that the given object conforms to the type Thing; however, a user can pass write a null or undefined, and a user won't know that they haven't handled it correctly.
This was one of the known tradeoffs when we implemented this feature - we acknowledged this. But I hit this surprisingly quickly. It seems like if you're using this pattern without classes, it's pretty easy to run into this issue.
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
Reproduziere das Problem mit dem bereitgestellten Thing-Interface und dem makeThing-Beispiel und untersuche anschließend die Property-Prüfung von TypeScript für Getter/Setter. Im Bericht wird weder eine Quelldatei noch ein Test genannt; für den weiteren Fortschritt müsste das beabsichtigte Verhalten festgelegt und eine Abdeckung für Schreibvorgänge mit null und undefined hinzugefügt werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100