-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Closed
Copy link
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
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.
gcnew
Metadata
Metadata
Assignees
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript