Description
TypeScript Version: 3.6.2
and 3.6.3
Search Terms: union types, intersection types, literal types
Code
type a = {
type: 'a',
data: string
}
type b = {
type: 'b',
name: string
}
type c = {
type: 'c',
other: string
}
type abc = a | b | c;
function f(problem: abc & (b | c)) {
if (problem.type === 'b') {
console.log(problem.name);
} else {
console.log(problem.other);
}
}
Expected behavior:
TypeScript not to complain about the typings.
Actual behavior:
In the f
function, TypeScript says that problem
may not have name
because type Property 'name' does not exist on type 'a & c'
, but a
and c
are mutually exclusive types and therefore not possible. It also complains that problem
may not have other
because Property 'other' does not exist on type 'a & b'
, but due to the if/else, it's not possible for problem
to be of type b
, and in fact the only possible type for it is c
.
Playground Link: Since the playground is still on 3.5.1, this issue is not reproducible there. Here's a link to the example I posted above in the playground (though it is passing): https://www.typescriptlang.org/play/#code/C4TwDgpgBAhlC8UDeAoK6qkgLigchjwBo0MATGYGXAZ2ACcBLAOwHMUBfFLaAIwWSl0PXHl7EhUZjAC2EWgxbsuPKAGMBqDJnDz8aidoD2wABYR6Cpm07ddsXhsRwAPlH5u1AbhQAzAK7MasCMRsxQvgAUYPRGvAA2EDK4MI5QAGRQkR7qAJS5gtqMvlkxcYkyAHSq8LX44gVa2hhqYTRGiZXxRqzRsQlJldJyuT7aHFAQ8TTQTc3qbR0QXT195YMm5vSjklxcQA
Related Issues: #33101 looked like it might be related, can't quite tell.