You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constenumChildType{A,B,C}interfaceChildrenMap{[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 istypeChild<TextendsChildType>={type: T}&ChildrenMap[T];interfaceParentMap{[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 havetypeParent<TextendsChild<any>>=TextendsChild<infer R>
? RextendsChildType ? ParentMap[R]&{child: Child<R>}
: never
: never;// More of the same stuff!interfaceGrandParent<TextendsParent<Child<any>>>{parent: TextendsParent<Child<infer R>>
? RextendsChildType ? Parent<Child<R>>
: never
: never;}// It all works as expected at this pointletA: Parent<Child<ChildType.A>>;letred: typeofA['color']='red';letjustRed: typeofA['color']='blue';// error!// Same sorta thing as ^letB: Parent<Child<ChildType.B>>;letC: Parent<Child<ChildType.C>>;functiontestParent<TextendsParent<any>>(parent: T){if(parent.child.type===ChildType.A){parent.child.value;// equals 10!parent.color;// red | green | blue, but should only be red}}functiontestGrandParent<TextendsGrandParent<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.
TypeScript Version: 3.4.5
Search Terms:
Code
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 statementparent.type === ChildType.A
correctly infers the value ofvalue
(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
andparent.child.value
, correctly coerce the appropriate type. Howevercolor
is incorrect"red" | "blue" | "green"
instead of one of the three.Playground Link:
link to the playground
Related Issues:
The text was updated successfully, but these errors were encountered: