No error when type guard claims the object will have a certain value at a key passed as a string param, but doesn't work. #17568
Labels
Working as Intended
The behavior described is the intended behavior; this is not a bug
Came across this while trying to come up with a workaround for #17567 .
TypeScript Version: 2.4.0-dev.20170607
Code
Expected behavior:
Ideally,
value.foo
has type[number, number]
, but if not, then an error onvalue is { [field]: [number, number] }
saying that this kind of type guard does not work.Actual behavior:
No errors for the
hasField
function at all, butvalue
has type{}
within theif (hasField(value, 'foo'))
block, resulting in an error onvalue.foo
stating that “Propertyfoo
does not exist on type{}
.”Note that the behavior is nearly identical if you have
In this case,
hasField
still has a return value (according to VS Code intellisense) ofvalue is {}
, andvalue.foo
has a type of[number, number] | null | undefined
. So the indexed definition ofA
doesn't actually affect this.The text was updated successfully, but these errors were encountered: