roblema de escopo estranho ao usar proteções tipográfic
Diga que temos este código de texto datilografado:
interface A {
bar: string;
}
const isA = <T>(obj: T): obj is T & A => {
obj['bar'] = 'world';
return true;
}
let obj = { foo: 'hello' };
if (!isA(obj)) throw 'wont ever throw'
obj.foo // This is ok
obj.bar // This is ok
Array(5).fill(0).forEach((_, i) => {
obj.foo // This is ok
obj.bar // This is not ok
});
Por que éobj.bar
inválido enquanto estiver dentro doforEach
?