Open Sainan opened 2 days ago
You can convert the type on your own pool.push(reward as IRngResult);
I know that casting is an option, but it's generally more desirable to prove than promise something to a compiler.
Duplicate of #42384.
I don't think these are quite the same issue, since in that case it's about inferring which variant of a union type is active in the given branch, whereas in this case, there is no union type, only an object and its properties.
It's about narrowing a property propagating to the parent type. You expect by checking the property probability
that your IRewad
type suddenly is compatible with IRngResult
, but that's not how narrowing works. Checking the property does not change the parent type.
Inheritance is involved nowhere here. Please don't speak if you have nothing of value to add.
I did not mention inheritance anywhere. Please don't say I have nothing of value to add when you don't understand the problem at hand. 🙃
Checking a property like probability
can be used to narrow the type of the object (what I referred to as parent) when it's a union type. It does not change the type of the parent from { probability?: number }
to { probability: number }
, that's not supported.
Type guards do not propagate type narrowings to parent objects. The narrowing is only applied upon access of the narrowed property which is why the destructing function works, but the reference function does not. Narrowing the parent would involve synthesizing new types which would be expensive. More detail in this comment.
🔎 Search Terms
property undefined propagation
🕗 Version & Regression Information
Not a regression as far as I can tell.
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240702#code/JYOwLgpgTgZghgYwgAgJICUIHc5QCbIDeAUMmcmAJ4AOEAXMgM5hSgDmA3KedVAPYAjOAOAAbYFQD8DEAFcAtgOhdyyKLgmVpTFuy4BfYsVCRYiFBhBtMjWaLBFuZKrQbNWVlT35CR4qjIKSlAGRgh8IMxq2Lh4AAp8fKIAKnw2dmAJScgAvMgAFFAx+IwMGMV4ANoAugCUZehW6fY1uQB8jqoA9F1oeBBwoqKUADQUABbAjMhYfHYEs1AA1nROyD1FYLJQINE4JQB0MGKm+QAe7chnB7yCwmKayACEOXmyIP3HIBB4tV5kPWQACFZGAxhAAG4QXagCYod78IY-ZAIOCMCBjCTIUDhKBFBBgYbIaD8KClNbhSIOaiJUQNJoQWwtaq5ZA1f7IGB8KAFSlRIr7Ah8GB7WKMWqdVTkYAiwoVG4+e7+SjPV7Id6fUA-CUkKV65A0pI3WSMcZywV-NaqQzWtabba7Q2iAxcIA
💻 Code
🙁 Actual behavior
It complains that the
probability
property may be undefined, even tho it was explicitly checked for that.🙂 Expected behavior
It accepts the
push
as valid.Additional information about the issue
No response