microsoft / microsoft/TypeScript
Conjunction of two disjunctions cause incorrect errors
Nobody has claimed this yet.
- 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.
Related Issues:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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