microsoft / microsoft/TypeScript

Cant infer type of parent based on member of child

Open
#31,608 1 comment 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Playground reproduction and the testParent and testGrandParent examples, then compare the behavior with typescript@next. The change is done when narrowing parent.child.type also narrows the containing parent color to the matching literal in both examples, with regression coverage added in the compiler test suite.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.