microsoft / microsoft/TypeScript

Cant infer type of parent based on member of child

Aperta
#31,608 1 commento 9 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 3.4.5

Search Terms:

  • infer parent based on child
  • type inference in if statement
  • conditional generic type infer

Code

const enum ChildType {
    A,
    B,
    C
}

interface ChildrenMap {
    [ChildType.A]: { value: 10 },
    [ChildType.B]: { value: string },
    [ChildType.C]: { value: boolean, test?: boolean }
};

// Create a "Child", that has a type and
// other members based on what type it is
type Child<T extends ChildType> = { type: T } & ChildrenMap[T];

interface ParentMap {
    [ChildType.A]: { color: 'red' },
    [ChildType.B]: { color: 'blue' },
    [ChildType.C]: { color: 'green' }
}

// Create a "Parent", that has a "Child", which
// has a type, which decides what members the 
// parent should have
type Parent<T extends Child<any>> =
    T extends Child<infer R>
    ? R extends ChildType ? ParentMap[R] & { child: Child<R> }
    : never 
    : never;

// More of the same stuff!
interface GrandParent<T extends Parent<Child<any>>> {
    parent: T extends Parent<Child<infer R>>
    ? R extends ChildType ? Parent<Child<R>>
    : never
    : never; 
}

// It all works as expected at this point
let A: Parent<Child<ChildType.A>>;
let red: typeof A['color'] = 'red';
let justRed: typeof A['color'] = 'blue'; // error!

// Same sorta thing as ^
let B: Parent<Child<ChildType.B>>;
let C: Parent<Child<ChildType.C>>;

function testParent<T extends Parent<any>>(parent: T) {
    if (parent.child.type === ChildType.A) {
        parent.child.value; // equals 10!
        parent.color; // red | green | blue, but should only be red
    }
}

function testGrandParent<T extends GrandParent<any>>(grandParent: T) {
    if (grandParent.parent.child.type === ChildType.B) {
        grandParent.parent.child.value; // string, yay!
        grandParent.parent.color; // red | green | blue, but should only be blue
    }
}

Expected behavior:
As the type of "Child" is being set using the same conditional type as the parent, the parent type should also get the appropriate type for "color".
In the case of testParent, the statement parent.type === ChildType.A correctly infers the value of value (in the case of that example 10). This should also happen for the top level member "color" as well (in the case of that example, color should be set to "red").

Actual behavior:
parent.child.type and parent.child.value, correctly coerce the appropriate type. However color is incorrect "red" | "blue" | "green" instead of one of the three.

Playground Link:

link to the playground

Related Issues:

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

Inizia dalla riproduzione collegata in Playground e dagli esempi testParent e testGrandParent, quindi confronta il comportamento con typescript@next. La modifica è completa quando il narrowing di parent.child.type restringe anche il colore del parent contenitore al literal corrispondente in entrambi gli esempi, con l'aggiunta della copertura di regressione nella suite di test del compilatore.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.