microsoft / microsoft/TypeScript
Suggestion: type narrowing based on user-defined type guards against properties
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
Search Terms
user-defined type guards, type predicates, type narrowing, object properties
Suggestion
TS can narrow object types in if statements and other conditional control flows by various means. Particularly, if the object's type is a union type distinguishable by a "tag" (like Option<T> in the below example), we can narrow the type by checking the value of the tag. However, if the check for the tag is done by user-defined type guards against the property (but not the object itself), the object's type is not narrowed.
The suggestion is to make it work even if user-defined type guards are applied to object properties
Use Cases
The "tagged union" pattern is widely used in TS codebases. If tags theirselves are so complicated, we want to utilize user-defined type guards to check them.
Examples
type Option<T> = {
type: "some"
value: T
} | {
type: "none"
}
const isSome = (type: "some" | "none"): type is "some" => type === "some"
declare const option: Option<number>
// Good: option is narrowed in the if block
if (option.type === "some") {
option.value
}
// Bad: option isn't narrowed
if (isSome(option.type)) {
// Error: Property 'value' does not exist on type 'Option<number>'.
option.value
}
// Available workaround
const isSomeObject = <T>(option: Option<T>): option is Extract<Option<T>, { type: "some" }> => option.type === "some"
if (isSomeObject(option)) {
option.value
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 mit dem verlinkten Playground und den Beispielen zu Option, um den Unterschied zwischen direkten Eigenschaftsprüfungen und benutzerdefinierten Typwächtern zu reproduzieren. Da im Issue keine Implementierungsdatei oder kein Test genannt wird, lokalisieren Sie die Control-Flow-Logik zur Typverengung und ihre vorhandenen Tests, bevor Sie die Änderung definieren. Als erledigt gilt die Änderung, wenn option.value nach isSome(option.type) akzeptiert wird, während das bestehende Verhalten der Typverengung unverändert bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100