TypeScript Version: 2.1.6
Code
// A *self-contained* demonstration of the problem follows...
interface Base {
readonly name: string;
}
interface DerivedA extends Base {
readonly name: 'DerivedA',
readonly A: string;
}
interface DerivedB extends Base {
readonly name: 'DerivedB',
readonly B: string;
}
type Derived = DerivedA | DerivedB;
type Both = Base & Derived;
// this function seems sufficient to hide the type from the compiler
function make(): Both {
return {
name: 'DerivedB',
B: 'b',
};
}
const derived = make();
// var name: (string & "DerivedB") | (string & "DerivedA")
if (derived.name === 'DerivedB') {
// Property 'B" does not exist on type 'Both'
console.log(derived.B);
}
Expected behavior:
I expect the type guard (derived.name === 'DerivedB') to narrow the type of derived from (Base & DerivedA) | (Base & DerivedB) to Base & DerivedB or DerivedB.
Actual behavior:
In the if block, derived.B is not available.
TypeScript Version: 2.1.6
Code
Expected behavior:
I expect the type guard
(derived.name === 'DerivedB')to narrow the type ofderivedfrom(Base & DerivedA) | (Base & DerivedB)toBase & DerivedBorDerivedB.Actual behavior:
In the
ifblock,derived.Bis not available.