-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 2.0.3
Code
type Cheese = {
type: 'brie';
brieOnly: true,
} | {
type: 'cheddar';
cheddarOnly: true,
}
function narrows<Wide extends string, Narrow extends Wide>(w: Wide, n: Narrow): w is Narrow {
return w === n;
}
function doSomethingWithCheese(cheeseSample: Cheese) {
if (cheeseSample.type === 'cheddar') {
cheeseSample.cheddarOnly // ok
}
if (narrows(cheeseSample.type, 'cheddar')) {
const t: 'cheddar' = cheeseSample.type; // has narrowed
cheeseSample.cheddarOnly // not ok, didn't narrow discriminated union
}
}Expected behavior:
Discriminated unions would work with type-guards in this way.
Actual behavior:
It doesn't, and I have to write a load of stuff like this in unit-tests:
if (cheese.type === 'cheddar') {
assert(/* some assertions using cheddar specific properties */);
} else {
throw Error("wrong type of cheese");
}rather than doing it in one step with an assertion throwing version of narrows:
if (assert.narrows(cheese.type, 'cheddar')) {
assert(/* some assertions using cheddar specific properties */);
}drew-r, seansfkelley, sladavich-iqvia, cnradich and Pttttn
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created