microsoft / microsoft/TypeScript
Types that depend on constants are not narrowing correctly
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
"ternary narrowing", "ternary constant", "constant narrowing", "constant dependent"
🕗 Version & Regression Information
- This is the behavior in every version I tried (including nightly), and I reviewed the FAQ.
⏯ Playground Link
💻 Code
const BOOL = true as const
type T_True = { x: string, T: string }
type T_False = { x: string, F: string }
type T = (typeof BOOL) extends true ? T_True : T_False
function f(): T {
const varT: T_True = { x: 'varT', T: 'good' }
const varF: T_False = { x: 'varF', F: 'okay' }
return BOOL ? varT : varF
}
🙁 Actual behavior
Typechecking fails with the error Type 'T_True | T_False' is not assignable to type 'T_True'. Property 'T' is missing in type 'T_False' but required in type 'T_True'. on the return statement.
🙂 Expected behavior
Typechecking should pass, since the const BOOL = true as const should mark the false ternary case as unreachable.
I also tried making T generic with respect to BOOL, and splitting the ternary operator into an if/else, but the same error persisted regardless.
Additional information about the issue
No response
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
Führe zunächst den verknüpften TypeScript Playground mit dem bereitgestellten BOOL-, Conditional-Type- und Ternary-Beispiel aus, um den Fehler bei der return-Anweisung zu reproduzieren. Verfolge das Verhalten der Typprüfung bei konstanten Bedingungen; abgeschlossen ist die Aufgabe, wenn das Beispiel typgeprüft wird und der false-Zweig als unerreichbar behandelt wird.
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