-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
Bug Report
π Search Terms
assert never branded string literal union type narrowing
π Version & Regression Information
This changed between versions 4.8.4 and 4.9.2-rc
β― Playground Link
π» Code
type Left = 'left';
type Right = 'right' & { right: 'right' };
type Either = Left | Right;
function assertNever(v: never): never {
throw new Error('never');
}
declare const value: Either;
if (value === 'left') {
const foo: 'left' = value;
} else if (value === 'right') {
const bar: 'right' = value; // correctly narrowed despite branding...
} else {
assertNever(value); // ...but not eliminated as a candidate
}Output
"use strict";
function assertNever(v) {
throw new Error('never');
}
if (value === 'left') {
const foo = value;
}
else if (value === 'right') {
const bar = value; // correctly narrowed despite branding...
}
else {
assertNever(value); // ...but not eliminated as a candidate
}Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}π Actual behavior
The compiler claims the assertNever is reachable (by virtue of only narrowing value to Right in the last branch).
π Expected behavior
The assertNever is considered unreachable (that is, value is narrowed to never).
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue