microsoft / microsoft/TypeScript

Type infer issue in interfaces with internal properties

Open
#42,716 5 comments 0 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Feb 10, 2021.

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

Description

Bug Report

🕗 Version & Regression Information

4.1.3 & 4.2.0-dev the same

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.2.0-beta#code/JYOwLgpgTgZghgYwgAgEJwM4oN4ChkHKgbAAmEAXMnobcgG5UgCuAtgEbQDc+hAvrgG5QkWIhQANZBAAekEKQxpMOXgWJlK1NXUbIAjDoG04TNpyg9aMgPxmO3QbmHho8JMgCa0uRAVL0LG1aDXIqGjoCPQAmIx1TZBYHSx0ATztE80chETdxZAAtH3lFZSCI9RASMODIhioAZjjaAC8MpIseIQQAeyqwBmj7C2QAXmRongAbCAH6fSp9AB9J52AYZAAKemix0fHogEoGfTGJrmkpoPmz-R5cXv6GOCnmLSklrzOKoirNcJ0tD08yMABp4osAAyCe6PDA9GYAOimPQA5tsXm9DjwgA

💻 Code
interface Base {
    inside: {
        v: number;
    }
}

interface X extends Base {
    inside: {
        v: 1
    }
    a: number;
    x?: number;
}

interface Y extends Base {
    inside: {
        v: 2
    }
    a: number;
    y?: number;
}

interface Z extends Base {
    inside: {
        v: 3
    }
    z?: number;
}

const v2: number = 2;
let v1: 1|2;

if (v2 === 2) v1 = 2; else v1 = 1;

const value: X | Y = {
    inside: {
        v: v1
    },
    a: 10
};

console.log(value);
🙁 Actual behavior

Type '{ inside: { v: 1 | 2; }; a: number; }' is not assignable to type 'X | Y'.
Type '{ inside: { v: 1 | 2; }; a: number; }' is not assignable to type 'Y'.
Types of property 'inside' are incompatible.
Type '{ v: 1 | 2; }' is not assignable to type '{ v: 2; }'.(2322)

🙂 Expected behavior

No error ^^

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.