Closed
Description
TypeScript Version: 3.5.1, 3.5.3
Search Terms: guard type-guard property return assignment
Code
type WideProp = {
prop: (number | string);
}
type NarrowProp = {
prop: string;
}
function showcase(): NarrowProp {
const wide: WideProp = {
prop: 'aaa' as (string | number)
};
const guard = (i: (number | string)): i is string => true;
if(guard(wide.prop)){
// this works
const prop: string = wide.prop;
// this does not?
let narrow: { prop: string } = wide;
// and this does not?
return wide;
}
}
Expected behavior:
Successful type guard check narrowing property prop
from string | number
to string
should allow us to return \ assign containing object instance when a { prop: string }
type is expected.
Actual behavior:
With successful type guard check we are allowed to assign property prop
when string
is expected, but assigning \ returning containing instance as { prop: string }
is still forbidden.
Related Issues: #3812
Metadata
Metadata
Assignees
Labels
No labels