microsoft / microsoft/TypeScript

Conjunction of two disjunctions cause incorrect errors

Open
#30,648 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

TypeScript Version: 3.3

Search Terms:

  • Conjunction

Code

interface Small {
    small: true,
    callbackWhenSmallIsFalsy?: undefined;
}

interface NotSmall {
    small?: false;
    callbackWhenSmallIsFalsy: () => void;
}

interface Green {
    green?: false
    numberWhenGreenTrue?: undefined
}

interface NotGreen {
    green: true
    numberWhenGreenTrue: number;
}

type SmallProps = Small | NotSmall;
type GreenProps = Green | NotGreen;

type Props = SmallProps & GreenProps;

const try1: Props = {
    green: true,
    numberWhenGreenTrue: 5,
    small: true
} // OK

const try2: Props = {
    green: false,
    numberWhenGreenTrue: 5,
    small: true
} // Unexpected Error: 'callbackWhenSmallIsFalsy' is missing...
// Expected Error: 5 is not assignable to undefined for 'numberWhenGreenTrue'

 const try3: Props = {
    green: false,
    small: true
} // OK

 const try4: Props = {
    green: false,
    small: true,
    callbackWhenSmallIsFalsy: () => 5
} // Acceptable Error: Types of property 'small' are incompatible.
// TS can't know which of the conditions to use, so this is okay. NTH: It lists multiple possible errors

Expected behavior:
in try3 I don't expect the error to be about callbackWhenSmallIsFalsy. I would expect an error about green or numberWhenGreenTrue

Actual behavior:
When the conjunction is evaluated as false, it appears that the compiler tries to force an error into the first part of the conjunction, when the falsehood is from the second part of the conjunction.

Playground Link:
http://www.typescriptlang.org/play/#src=interface%20Small%20%7B%0A%20%20%20%20small%3A%20true%2C%0A%20%20%20%20callbackWhenSmallIsFalsy%3F%3A%20undefined%3B%0A%7D%0A%0Ainterface%20NotSmall%20%7B%0A%20%20%20%20small%3F%3A%20false%3B%0A%20%20%20%20callbackWhenSmallIsFalsy%3A%20()%20%3D%3E%20void%3B%0A%7D%0A%0Ainterface%20Green%20%7B%0A%20%20%20%20green%3F%3A%20false%0A%20%20%20%20numberWhenGreenTrue%3F%3A%20undefined%0A%7D%0A%0Ainterface%20NotGreen%20%7B%0A%20%20%20%20green%3A%20true%0A%20%20%20%20numberWhenGreenTrue%3A%20number%3B%0A%7D%0A%0Atype%20SmallProps%20%3D%20Small%20%7C%20NotSmall%3B%0Atype%20GreenProps%20%3D%20Green%20%7C%20NotGreen%3B%0A%0Atype%20Props%20%3D%20SmallProps%20%26%20GreenProps%3B%0A%0Aconst%20try1%3A%20Props%20%3D%20%7B%0A%20%20%20%20green%3A%20true%2C%0A%20%20%20%20numberWhenGreenTrue%3A%205%2C%0A%20%20%20%20small%3A%20true%0A%7D%20%2F%2F%20OK%0A%0Aconst%20try2%3A%20Props%20%3D%20%7B%0A%20%20%20%20green%3A%20false%2C%0A%20%20%20%20numberWhenGreenTrue%3A%205%2C%0A%20%20%20%20small%3A%20true%0A%7D%20%2F%2F%20Unexpected%20Error%3A%20'callbackWhenSmallIsFalsy'%20is%20missing...%0A%2F%2F%20Expected%20Error%3A%205%20is%20not%20assignable%20to%20undefined%20for%20'numberWhenGreenTrue'%0A%0A%20const%20try3%3A%20Props%20%3D%20%7B%0A%20%20%20%20green%3A%20false%2C%0A%20%20%20%20small%3A%20true%0A%7D%20%2F%2F%20OK%0A%0A%20const%20try4%3A%20Props%20%3D%20%7B%0A%20%20%20%20green%3A%20false%2C%0A%20%20%20%20small%3A%20true%2C%0A%20%20%20%20callbackWhenSmallIsFalsy%3A%20()%20%3D%3E%205%0A%7D%20%2F%2F%20Acceptable%20Error%3A%20Types%20of%20property%20'small'%20are%20incompatible.%0A%2F%2F%20TS%20can't%20know%20which%20of%20the%20conditions%20to%20use%2C%20so%20this%20is%20okay.%20NTH%3A%20It%20lists%20multiple%20possible%20errors

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 by reproducing the conjunction example from the issue in the TypeScript Playground, especially try2 and try3, and inspect the compiler's type-checking and diagnostic behavior. Done means the error for try2 points to numberWhenGreenTrue or green rather than callbackWhenSmallIsFalsy, while the other shown cases retain their expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.