Closed
Description
π Search Terms
- switch
- narrowing
π Version & Regression Information
This is not a regression, it never worked.
β― Playground Link
π» Code
interface Base {
type: 'foo' | 'bar'
}
interface Foo extends Base {
type: 'foo';
foo: string;
}
interface Bar extends Base {
type: 'bar';
bar: number;
}
function getV(): Foo | Bar {
return null!;
}
const v = getV();
if((v.type) === 'bar') {
v.type
}
// Works
switch (v.type) { // <===
case 'bar':
v.bar;
break;
case 'foo':
v.foo;
break;
}
// Does not work
switch ((v.type)) { // <===
case 'bar':
v.bar; // KO
break;
case 'foo':
v.foo; // KO
break;
}
π Actual behavior
With extra braces in the switch
, narrowing is broken.
switch ((v.type)) { // <=== extra braces
case 'bar':
console.log(v.bar);
break;
case 'foo':
console.log(v.foo);
break;
}
π Expected behavior
switch ((v.type))
should behave as switch (v.type)
.
There is no such issue on a if
condition.
Metadata
Metadata
Assignees
Labels
No labels