microsoft / microsoft/TypeScript

3.5 regression: return type forced more restrictive than needed

Aperta
#31,316 7 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: check: Control Flow Domain: Indexed Access Types Needs Proposal Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci l'esempio di funzione generica con TypeScript 3.5.0-dev.20190508 e confrontane il comportamento con TypeScript 3.4, concentrandoti su come lo switch su T determina il tipo restituito indicizzato. Il lavoro è completato quando i rami array e Box vengono accettati senza il fallback irraggiungibile, mentre le chiavi non valide continuano a generare errori.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.