microsoft / microsoft/TypeScript

3.5 regression: return type forced more restrictive than needed

Offen
#31,316 7 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Control Flow Domain: Indexed Access Types Needs Proposal Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TypeScript Version: 3.5.0-dev.20190508

Search Terms: restrictive return type

Code

class Box<A> {
    constructor(public val: A) {}
}

interface ContainerFor<A> {
    array: Array<A>;
    box: Box<A>;
}

function single<T extends keyof ContainerFor<number>>(t: T, x: number): ContainerFor<number>[T] {
    switch (t) {
        case "array":
            return new Array<number>(x);
        case "box":
            return new Box<number>(x);
    }
    return new Box<number>(0); // this should not be needed
}

function test() {
    let t1 = single("array", 42);
    let t2 = single("box", 42);
    //let t3 = single("boo", 42);
}

Expected behavior:
This code worked with 3.4, but had an unsoundness bug in that single was allowed to return the wrong type (swap array and box). It was clear that switching over t did not increase knowledge about the expected output type and I was forced to have a fallback return which is of course unreachable. But the code itself worked.

The test function also shows things work as expected. The types for t1 and t2 are inferred correctly and asking for an unknown thing ("boo") is reported as an error.

I would expect this to still work in 3.5, perhaps fixing one of the issues I'm having in the internals of single.

Actual behavior:
Typescript 3.5 no longer accepts this code. returning new Array or new Box are flagged as wrong return types. The newly expected return type is now mandated to be the intersection type: number[] & Box<number>, which is too restrictive as the correct output type can be statically determined from T in the input. The fact that test correctly infers the output type is proof of this.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Beispiel für eine generische Funktion mit TypeScript 3.5.0-dev.20190508 und vergleiche sein Verhalten mit TypeScript 3.4, wobei der Fokus darauf liegt, wie der switch über T den indizierten Rückgabetyp bestimmt. Erledigt bedeutet, dass die array- und Box-Zweige ohne den unerreichbaren Fallback akzeptiert werden, während ungültige Schlüssel weiterhin Fehler bleiben.

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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.