Microsoft/TypeScript

Multiple sets of mutually exclusives properties lead to wrong compiler error

Open

#49,528 opened on 2022幎6月13日

GitHub で芋る
 (1 comment) (0 reactions) (0 assignees)TypeScript (6,726 forks)batch import
Experience EnhancementHelp WantedSuggestion

Repository metrics

Stars
 (48,455 stars)
PR merge metrics
 (平均マヌゞ 6d 17h) (30d で 9 merged PRs)

説明

Bug Report

🔎 Search Terms

type union interface mutually exclusive property never

🕗 Version & Regression Information

I stumble on it today. Happen in the nightly. Happen from ES3 to ESNext on the playground.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about a bug with properties of type never.

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Person
{
    kind: "Person";
    name: string;
}

interface Company
{
    kind: "Company";
    name: string;
}

// Fragments
interface _fromPerson
{
    fromPerson: Person;
    fromCompany?: never;
}

interface _fromCompany
{
    fromPerson?: never;
    fromCompany: Company;
}

interface _toPerson
{
    toPerson: Person;
    toCompany?: never;
}

interface _toCompany
{
    toPerson?: never;
    toCompany: Company;
}

interface RawMessage
{
    timestamp: number;
    title: string;
    content: string;
}

// Combined type
type Message = (
    RawMessage
    & (_fromPerson | _fromCompany)
    & (_toPerson | _toCompany)
);

const MESSAGE: Message = {
    timestamp: 1577836800000,
    title: "Bug",
    content: "This is an example",
    fromPerson: {
        kind: "Person",
        name: "Alice"
    },
    toPerson: {
        kind: "Person",
        name: "Bob"
    },
    toCompany: {
        kind: "Company",
        name: "ACME"
    },
};

🙁 Actual behavior

toPerson and toCompany are mutually exclusive, but the compiler report as error that property fromCompany is missing while fromPerson is already there and mutually exclusive with fromCompany.

(ts2322)
Type '{ ... }' is not assignable to type 'Message'.
  Type '{ ... }' is not assignable to type 'RawMessage & _fromCompany & _toCompany'.
    Property 'fromCompany' is missing in type '{ ... }' but required in type '_fromCompany'.

If you only have 1 set of mutually exclusive interfaces, you get a correct error :

(ts2322)
Type '{ ... }' is not assignable to type 'Message'.
  Type '{ ... }' is not assignable to type 'RawMessage & _toCompany'.
    Type '{ ... }' is not assignable to type '_toCompany'.
      Types of property 'toPerson' are incompatible.
        Type '{ kind: "Person"; name: string; }' is not assignable to type 'never'.

🙂 Expected behavior

The compiler do not mention fromCompany, but mention toPerson and toCompany being mutually exclusive.

(ts2322)
Type '{ ... }' is not assignable to type 'Message'.
  Type '{ ... }' is not assignable to type 'RawMessage & _toCompany'.
    Type '{ ... }' is not assignable to type '_toCompany'.
      Types of property 'toPerson' are incompatible.
        Type '{ kind: "Person"; name: string; }' is not assignable to type 'never'.

コントリビュヌタヌガむド